I want to fill NAs of my dataset in group variables according to the values of the group in previous years of that ID itself.
the na.locf(newData, na.rm = TRUE)
part of code does not work. I think it is because the input is not a number. Or is it another thing?
Does anyone know how to fix this problem?
for (i in my_data$ID){
newData = my_data[my_data$ID==i,c('ID','Year', 'group')][3]
na.locf(newData,na.rm = TRUE)
}
my dataset is very big. but I provide this as a sample of what I need:
structure(list(ID = c(1L, 2L, 3L, 1L, 1L, 1L), Year = c(2000L,
2000L, 2001L, 2001L, 2002L, 2003L), Group = structure(c(2L, 3L,
2L, 1L, 1L, 4L), .Label = c("", "\"A\"", "\"B\"", "\"C\""), class = "factor")), row.names = c(NA,
6L), class = "data.frame")
the result should be like this:
structure(list(ID = c(1L, 1L, 1L, 1L, 2L, 2L), Year = c(2000L,
2001L, 2002L, 2003L, 2000L, 2002L), Group = structure(c(1L, 1L,
1L, 3L, 2L, 2L), .Label = c("\"A\"", "\"B\"", "\"C\""), class = "factor")), row.names = c(NA,
6L), class = "data.frame")