Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fill column with previous column if NA

Tags:

dataframe

r

dplyr

I have a data frame like this

df <- data.frame(v1 = 10:14, v2 = c(NA, 1, NA, 3, 6), v3 = c(1, NA, NA, 9, 4))

  v1 v2 v3
1 10 NA  1
2 11  1 NA
3 12 NA NA
4 13  3  9
5 14  6  4

I now want to fill the NAs with the value of the previous column, so it looks like this:

  v1 v2 v3
1 10 10  1
2 11  1  1
3 12 12 12
4 13  3  9
5 14  6  4

I know how to do this manually, like this:

df$v2 <- ifelse(is.na(df$v2), df$v1, df$v2)

How can I automate this for a full data frame with many columns?

like image 223
spore234 Avatar asked Nov 17 '17 19:11

spore234


People also ask

How do I change column values based on conditions in pandas?

You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.


1 Answers

You can do this with fill from tidyr:

library(dplyr)
library(tidyr)

data.frame(t(df)) %>%
  fill(., names(.)) %>%
  t()

Result:

   v1 v2 v3
X1 10 10  1
X2 11  1  1
X3 12 12 12
X4 13  3  9
X5 14  6  4

Note:

Basically, I transposed df, filled every column downward, then transposed it back to the original orientation

like image 140
acylam Avatar answered Sep 29 '22 07:09

acylam