1
我對患者ID和他們是否經歷過干預的二元指標有一個df。我想創建一個名爲「time_post」的新列,它告訴我經歷了干預後有多少時間點已經過去。將COL值分配給在不同列中觀察到的第一,第二,第三個值
這裏是我的DF:
names<-c("tom","tom","tom","tom","tom","tom","tom","tom", "john", "john","john", "john","john", "john","john", "john")
post<-as.numeric(0,0,0,1,1,1,1,1,0,1,1,1,1,1,1,1)
df<-data.frame(names,post)
這是我曾嘗試:
df$time_post<-ifelse(df$post==1[1],1,0) ##this tries to assign 1 to "time_post" for first value of 1 seen in post
df$time_post<-ifelse(df$post==1[2],2,df$time_post) ##trying to apply same logic here, but doesn't work. Introduces NAs into time_post column.
這是我想要的輸出;
names post time_post
1 tom 0 0
2 tom 0 0
3 tom 0 0
4 tom 1 1
5 tom 1 2
6 tom 1 3
7 tom 1 4
8 tom 1 5
9 john 0 0
10 john 1 1
11 john 1 2
12 john 1 3
13 john 1 4
14 john 1 5
15 john 1 6
16 john 1 7
預先感謝您
完美的作品,謝謝托馬斯。我也在考慮Colsums,但不知道如何寫它。 – user2363642