Search code examples
rstringrows

Replacing empty quotes with latest non-empty character


The question is: how to fill empty quotes with previous row value in r? I have the following character array:

a=c("hello", "", "", "g_joy", "hello_w", "", "", "", "baby__", "rose", "samanthaberry11", 
    "eltonjames", "", "", "andrewger", "Ironman", "cec_sabry")

The desired result is, in this case:

>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"      

I was thinking of filling the vector using reduce:

xx = Reduce(function(x,y) if (y==' ') x else y, a)
b=cbind(xx,a)

Anyway, I did not obtain the desired result (the obtained result is in the first column):

> 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"

Solution

  • Try the base R code below

    > 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"