R 用最新的非空字符替换空引号

R 用最新的非空字符替换空引号,r,string,rows,R,String,Rows,问题是:如何用r中的前一行值填充空引号?我有以下字符数组: a=c("hello", "", "", "g_joy", "hello_w", "", "", "", "baby__", "rose", "samanthaberry11", "eltonjame

问题是:如何用
r
中的前一行值填充空引号?我有以下字符数组:

a=c("hello", "", "", "g_joy", "hello_w", "", "", "", "baby__", "rose", "samanthaberry11", 
    "eltonjames", "", "", "andrewger", "Ironman", "cec_sabry")
在这种情况下,期望的结果是:

>a
[1] "hello"           "hello"                "hello"                "g_joy"          
[5] "hello_w"         "hello_w"                "hello_w"                "hello_w"               
[9] "baby__"          "rose"            "samanthaberry11" "eltonjames"     
[13] "eltonjames"                "eltonjames"                "andrewger"       "Ironman"        
[17] "cec_sabry"      
我正在考虑使用
reduce
填充向量:

xx = Reduce(function(x,y) if (y==' ') x else y, a)
b=cbind(xx,a)
无论如何,我没有得到想要的结果(得到的结果在第一列):

图书馆(动物园)
#将“”替换为
a[a==“”]
库(动物园)
#将“”替换为
a[a==“”]这就做到了:


图书馆(动物园)
`is.na%na.locf(na.rm=FALSE)
使用
is.na可以实现以下功能:


图书馆(动物园)
`is.na%na.locf(na.rm=FALSE)

使用
is.na尝试下面的基本R代码

> Filter(nchar, a)[cumsum(!!nchar(a))]
 [1] "hello"           "hello"           "hello"           "g_joy"
 [5] "hello_w"         "hello_w"         "hello_w"         "hello_w"
 [9] "baby__"          "rose"            "samanthaberry11" "eltonjames"
[13] "eltonjames"      "eltonjames"      "andrewger"       "Ironman"
[17] "cec_sabry"

请尝试下面的基本R代码

> Filter(nchar, a)[cumsum(!!nchar(a))]
 [1] "hello"           "hello"           "hello"           "g_joy"
 [5] "hello_w"         "hello_w"         "hello_w"         "hello_w"
 [9] "baby__"          "rose"            "samanthaberry11" "eltonjames"
[13] "eltonjames"      "eltonjames"      "andrewger"       "Ironman"
[17] "cec_sabry"

您正在测试的是
'
不是
'
减少(函数(x,y)如果(y='')x否则y,a,accumulate=TRUE)
分号是这些->;单引号是这些->“双引号是这些->”@1kmonkeysandasinglePC:谢谢!我编辑了你测试的帖子
'
不是
'
减少(函数(x,y)如果(y='')x,否则y,a,accumulate=TRUE)
分号是这些->;单引号是这些->“双引号是这些->”@1KMonkeys和SinglePC:谢谢!我编辑了这篇文章
> Filter(nchar, a)[cumsum(!!nchar(a))]
 [1] "hello"           "hello"           "hello"           "g_joy"
 [5] "hello_w"         "hello_w"         "hello_w"         "hello_w"
 [9] "baby__"          "rose"            "samanthaberry11" "eltonjames"
[13] "eltonjames"      "eltonjames"      "andrewger"       "Ironman"
[17] "cec_sabry"