1

I would like to populate the next two NA rows (and only the next two rows) of the variable "value" in a dataframe "df", grouped by individual. I tried with na.locf (using tidyr/dplyr packages) but did not manage to populate only the next two rows.

Dataframe:

library(data.table)
df <- data.table(data.frame("indiv"=c(1,1,1,1,2,2,2,2,2,3,3,3,3),"value" =c(5,NA,NA,NA,NA,8,NA,NA,NA,7,NA,NA,NA)))

Desired output:

data.frame("indiv"=c(1,1,1,1,2,2,2,2,2,3,3,3,3),"value" =c(5,5,5,NA,NA,8,8,8,NA,7,7,7,NA))

Thank you very much for your help.

Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
Dadoo
  • 51
  • 1
  • 1
  • 5
  • If you feel coercing the data to/from `zoo` object is not a viable option for you, feel free to ping me and I will vote to reopen your question. – Roman Luštrik Feb 24 '19 at 12:32

0 Answers0