0

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!

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Gregor, but it seems that the values changed. Look at country c for example.
Sorry, needed to transpose the result. Fixed now.
By the way Gregor, if I had the data in columns, like column country, year and data. How would I fill the missing values, BY country? Thanks!
Generally, you can use dplyr or data.table to easily do anything by group. Here's a complete question on filling NA by group: stackoverflow.com/q/23340150/903061

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.