NoobR NoobR - 2 months ago 6
R Question

copying character data down rows

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.

Answer

This should do it:

library(zoo)
data$Site[data$Site == ""] <- NA
data$Site2 <- na.locf(data$Site,na.rm = FALSE)