Say I have longitidunal dataset (df) consisting of twelve waves with four variables and two individuals in the long format: -
id<- c("1", "1", "1", "1","1", "1","1", "1","1", "1","1", "1", "2", "2", "2", "2", "2", "2","2", "2", "2","2", "2", "2") wave<- c("a", "b", "c", "d","e", "f", "g", "h", "i", "j","k", "l", "a", "b", "c", "d","e", "f", "g", "h", "i", "j","k", "l") country<- c("Bornuk", "NA","NA", "NA","NA", "NA","NA", "NA","NA", "NA", "NA", "NA", "Notbornuk", "NA","NA", "NA","NA", "NA","NA", "NA","NA", "NA", "NA", "NA") education <- c("not educated", "not educated", "not educated", "not educated", "not educated", "not educated", "not educated", "not educated", "not educated", "not educated", "not educated", "not educated", "educated", "educated", "educated", "educated", "educated", "educated", "educated", "educated", "educated", "educated", "educated", "educated")
df <- as.data.frame(cbind(id, wave, country, education))
The data is the long format id wave country 1 1 1 1... 2 2 2 2 2...
wave(a,b,c,d etc) represent different timepoints. id (1,2) in the unique identifier country shows person 1 was born in the UK but this is not repeated in subsequent waves, same for person 2 who was not born in the UK education shows person 1 is not educated and this information remains the same throughout the waves, person 2 is educated and the information is the same across the waves.
The country variable data was only collected in wave one. Not repeated in other waves. I would like to fill this info from wave b to k by repeating data from wave one.
I thought of using coalesce but I am not sure how. I tried: -
df <- df %>%
arrange(id, wave) %>%
tidyr::fill(country, .direction ="down")
It gave nothing. Please assist!