0

I have a data frame like this:

var1   var2
a      1
b      NA
c      NA
d      2
e      NA
f      3
g      3

I need the following data frame

var1   var2
a      1
b      1
c      1
d      2
e      2
f      3
g      3

I need the way of replacing NA value with last no-NA value of the column var2.

Thank you for your help.

  • possible duplicate: https://stackoverflow.com/q/7735647/5977215 ?, or this one: https://stackoverflow.com/q/2776135/5977215 ? – SymbolixAU Feb 12 '19 at 01:33

1 Answers1

0

Use the fill function from the tidyr package

library(tidyr)
df = df %>% fill(var2)

where df is the name of your dataframe.

Output:

var1 var2
a    1
b    1
c    1
d    2
e    2
f    3
g    3
sumshyftw
  • 1,111
  • 6
  • 14