Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

r - Add missing value in column with value from row above

Every week I a incomplete dataset for a analysis. That looks like:

df1 <- data.frame(var1 = c("a","","","b",""), 
             var2 = c("x","y","z","x","z"))

Some var1 values are missing. The dataset should end up looking like this:

df2 <- data.frame(var1 = c("a","a","a","b","b"), 
             var2 = c("x","y","z","x","z"))

Currently I use an Excel macro to do this. But this makes it harder to automate the analysis. From now on I would like to do this in R. But I have no idea how to do this.

Thanks for your help.

QUESTION UPDATE AFTER COMMENT

var2 is not relevant for my question. The only thing I am trying to is. Get from df1 to df2.

df1 <- data.frame(var1 = c("a","","","b",""))
df2 <- data.frame(var1 = c("a","a","a","b","b"))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...