Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add missing value in column with value from row above

Tags:

r

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"))
like image 683
jeroen81 Avatar asked Mar 01 '12 10:03

jeroen81


People also ask

How do you fill missing values in a data set?

Use the fillna() Method: The fillna() function iterates through your dataset and fills all null rows with a specified value. It accepts some optional arguments—take note of the following ones: Value: This is the value you want to insert into the missing rows. Method: Lets you fill missing values forward or in reverse.

Which function is used for filling NA value with consecutive values in R?

The fillna() function is used to fill NA/NaN values using the specified method.


2 Answers

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
like image 136
Andrie Avatar answered Oct 14 '22 10:10

Andrie


Here is a simpler way:

library(zoo)
df1$var1[df1$var1 == ""] <- NA
df1$var1 <- na.locf(df1$var1)
like image 26
Andrei Avatar answered Oct 14 '22 08:10

Andrei