0

I have a ridiculous For loop question, and I apologize in advance, but I cannot figure it out. Here is a table:

     Site     Date Software EPFU LABO
3  Site 10 20160517     BCID   70  210
4  Site 10 20160518     BCID   NA   NA
5          20160519     BCID   NA   NA
6          20160520     BCID   NA   NA
7          20160521     BCID   NA   NA
8          20160522     BCID   NA   NA
9  Site 11 20160517     BCID    2  139
10 Site 11 20160518     BCID   NA    9
11         20160519     BCID   NA   NA
12         20160520     BCID   NA   NA
13         20160521     BCID   NA   NA
14         20160522     BCID   NA   NA`

How do I fill the gaps below "Site 10" with text that says "Site 10" until it reaches "Site 11", at which point the cycle repeats with the text "Site 11" and so on...Any help would be appreciated. Site is currently a character class, but I can make it a factor if that will simplify this mess. One additional problem is that the "blank" cells are actually a range of ""," ", and "*"anything that will simplify this mess.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
NoobR
  • 311
  • 2
  • 10
  • I had (and un-did) the "duplicate" of http://stackoverflow.com/questions/14655286/replace-missing-value-with-previous-value (which is, in essence, @joy's solution below) since you need to do the extra step of turning those blanks into `NA`s before you can just use `na.locf()` – hrbrmstr Sep 16 '16 at 18:41

2 Answers2

3

This should do it:

library(zoo)
data$Site[data$Site == ""] <- NA
data$Site2 <- na.locf(data$Site,na.rm = FALSE)
Dan Slone
  • 543
  • 2
  • 8
2

na.locf() is your friend. It is described as "Generic function for replacing each NA with the most recent non-NA prior to it."

Try:

install.packages("zoo")
df$Site<-zoo::na.locf(df$Site)
Joy
  • 769
  • 6
  • 24