1

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"
zx8754
  • 52,746
  • 12
  • 114
  • 209
Mark
  • 1,577
  • 16
  • 43

3 Answers3

3

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"
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
1
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"      
Wimpel
  • 26,031
  • 1
  • 20
  • 37
1

This does the trick:


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

Using the is.na<- function like so is awkward, but it does offer a way to do this in one code line, which is nice for chaining.

It was designed to be used as demonstrated here.

Sirius
  • 5,224
  • 2
  • 14
  • 21