Bruno Guarita
Bruno Guarita

Reputation: 817

fill missing values with value from previous column

I have a data.frame whit some columns with missing values, and I want that the missing values are filled in with data from a previous column. For example:

country <- c('a','b','c')
yr01 <- c(15,16,7)
yr02 <- c(NA,18,NA)
yr03 <- c(20,22,NA)
yr04 <- c(15,18,19)

tab <- data.frame(country,yr01,yr02,yr03,yr04)
tab

  country yr01 yr02 yr03 yr04
1       a   15   NA   20   15
2       b   16   18   22   18
3       c    7   NA   NA   19

How can I make it so that the NA are replaced by the previous value? For example, in country a column yr02 will be equals to 15, and in country c columns year02 and yr03 will be 7. Thanks!

Upvotes: 0

Views: 188

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 146164

It's usually easier to work with columns, but we can apply to rows the standard answer from the R-FAQ Replace NAs with latest non-NA value.

tab[-1] = t(apply(tab[-1], 1, zoo::na.locf))
tab
#   country yr01 yr02 yr03 yr04
# 1       a   15   15   20   15
# 2       b   16   18   22   18
# 3       c    7    7    7   19

Upvotes: 2

Related Questions