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

Replacing empty quotes with latest non-empty character

问题是:如何用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)

反正我没有得到想要的结果(得到的结果在第一列):

> b
     xx          a                
[1,] "cec_sabry" "hello"          
[2,] "cec_sabry" ""               
[3,] "cec_sabry" ""               
[4,] "cec_sabry" "g_joy"          
[5,] "cec_sabry" "hello_w"        
[6,] "cec_sabry" ""               
[7,] "cec_sabry" ""               
[8,] "cec_sabry" ""               
[9,] "cec_sabry" "baby__"         
[10,] "cec_sabry" "rose"           
[11,] "cec_sabry" "samanthaberry11"
[12,] "cec_sabry" "eltonjames"     
[13,] "cec_sabry" ""               
[14,] "cec_sabry" ""               
[15,] "cec_sabry" "andrewger"      
[16,] "cec_sabry" "Ironman"        
[17,] "cec_sabry" "cec_sabry"
library(zoo)
# Replace "" with <NA>
a[a == ""] <- NA
# Fill NA with last known value, keep leading NA's
na.locf(a, na.rm = FALSE)

# [1] "hello"           "hello"           "hello"           "g_joy"           "hello_w"         "hello_w"        
# [7] "hello_w"         "hello_w"         "baby__"          "rose"            "samanthaberry11" "eltonjames"     
# [13] "eltonjames"      "eltonjames"      "andrewger"       "Ironman"         "cec_sabry"      

这样就可以了:


library(zoo)
`is.na<-`( a, a== "" ) %>% na.locf(na.rm=FALSE)

像这样使用 is.na<- 函数很尴尬,但它确实提供了一种在一个代码行中执行此操作的方法,这非常适合链接。

它被设计为按演示使用 here

试试下面的基本 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"