0

Hi I have the following data:

test=data.frame(Instrument=c(1,1,1,1,1),Code=c(278865100,NA,NA,278865101,NA),Date=c(2009,2010,2011,2012,2013))

I'd like to fill the first two NA rows with 278865100, and fill the last NA with 278865101. Basically, my goal is to fill NA with the most recent data for a specific instrument number.

Thank you!

Jane
  • 91
  • 4

1 Answers1

1

onyambu's comment is spot on here - fill is what you want and the suggestion works fine. There is more granular control on this function if needed, allowing you to fill upwards or downwards. Another approach:

library(tidyverse)

test=data.frame(Instrument=c(1,1,1,1,1),Code=c(278865100,NA,NA,278865101,NA),Date=c(2009,2010,2011,2012,2013))

#call the column named Code and fill downward.
test %>% fill(Code, .direction = 'down')
Peter_Evan
  • 947
  • 10
  • 17