In df
I would like to replace the NA values with the non-NA value for each id
id<-c(1,1,1,1,2,2,2)
price<-c(NA,20,NA,NA,NA,NA,5)
df<-data.frame(id,price)
id price
1 NA
1 20
1 NA
1 NA
2 NA
2 NA
2 5
The output should ideally look like:
id price
1 20
1 20
1 20
1 20
2 5
2 5
2 5
Any help would be appreciated.