Every week I a incomplete dataset for a analysis. That looks like:
df1 <- data.frame(var1 = c(\"a\",\"\",\"\",\"b\",\"\"),
var2 = c(\"x\",\"
Here is one way of doing it by making use of run-length encoding (rle
) and its inverse rle.inverse
:
fillTheBlanks <- function(x, missing=""){
rle <- rle(as.character(x))
empty <- which(rle$value==missing)
rle$values[empty] <- rle$value[empty-1]
inverse.rle(rle)
}
df1$var1 <- fillTheBlanks(df1$var1)
The results:
df1
var1 var2
1 a x
2 a y
3 a z
4 b x
5 b z