I'm trying to make survival dataset with event and time.
The dataset is assessed four times repeatedly. firs means the first observation, seco means the second observation ... and so on.
Event is defined as that observed value become the half of first observation.
The time is the time when event happen, so if 5 in firs become 2 in thir, the time is 3.
If no event happens until four, event is 0 and time is 4.
If missing happens, event is 0 and time is that time.
set.seed(1234)
firs<-sample(x=1:10,size=10)
seco<-sample(x=1:10,size=10)
thir<-sample(x=1:10,size=10)
four<-sample(x=1:10,size=10)
set<-as.data.frame(cbind(firs,seco,thir,four))
set$crit<-set$firs/2
set[2,3]<-NA
set[9,2]<-NA
set
firs seco thir four crit
1 2 7 4 5 1.0
2 6 5 NA 3 3.0
3 5 3 2 9 2.5
4 8 10 1 4 4.0
5 9 2 8 2 4.5
6 4 9 5 7 2.0
7 1 6 9 1 0.5
8 7 1 10 10 3.5
9 10 NA 6 6 5.0
10 3 4 7 8 1.5
So, in this dataset, the time and event will be
time event
1 4 0
2 3 0
3 3 1
4 3 1
5 2 1
... and so on
I'm trying to make code with for and if statement. However, it generated error.
for example,
for(i in 2:4){
if(set[,i]<set$crit){
set$event<-1
set$time<-i-1
}else if(set[,i]>set$crit)
{set$event<-0
set$time<-i}
}
Warning messages:
1: In if (set[, i] < set$crit) { :
the condition has length > 1 and only the first element will be used
2: In if (set[, i] > set$crit) { :
the condition has length > 1 and only the first element will be used
3: In if (set[, i] < set$crit) { :
the condition has length > 1 and only the first element will be used
4: In if (set[, i] > set$crit) { :
the condition has length > 1 and only the first element will be used
5: In if (set[, i] < set$crit) { :
the condition has length > 1 and only the first element will be used
6: In if (set[, i] > set$crit) { :
the condition has length > 1 and only the first element will be used
Would you let me know what was the problem and how to solve it?
Aucun commentaire:
Enregistrer un commentaire