I have a table in long format where data that is constant over time appears just once by id, like this
dt<-data.table(
ID=c(rep(1,3),rep(2,3)),
Time=rep(1:3,2),
Sex=c("M",NA,NA,"F",NA,NA),
V1=c(1.1,1.2,1.3,2.1,2.2,2.3)
)
dt
ID Time Sex V1
1: 1 1 M 1.1
2: 1 2 <NA> 1.2
3: 1 3 <NA> 1.3
4: 2 1 F 2.1
5: 2 2 <NA> 2.2
6: 2 3 <NA> 2.3
and I want to fill the missing values in Sex with the values of Sex when Time==1, that is, to repeat values across Time moments by ID, so that dt become
ID Time Sex V1
1: 1 1 M 1.1
2: 1 2 M 1.2
3: 1 3 M 1.3
4: 2 1 F 2.1
5: 2 2 F 2.2
6: 2 3 F 2.3
How can I achieve that?
Thanks