R'中的错误;对于';和';如果';生存数据集生成中的语句

R'中的错误;对于';和';如果';生存数据集生成中的语句,r,for-loop,if-statement,R,For Loop,If Statement,我试图用事件和时间制作生存数据集 数据集重复评估四次firs表示第一次观察,seco表示第二次观察。。。等等 事件定义为观测值成为第一次观测值的一半 时间是事件发生的时间,因此,如果第一个中的5变为第三个中的2,则时间为3 如果在4之前没有事件发生,则事件为0,时间为4 若发生丢失,则事件为0,时间为该时间 set.seed(1234) firs<-sample(x=1:10,size=10) seco<-sample(x=1:10,size=10) thir<-sample(

我试图用事件和时间制作生存数据集

数据集重复评估四次
firs
表示第一次观察,
seco
表示第二次观察。。。等等

事件定义为观测值成为第一次观测值的一半

时间是事件发生的时间,因此,如果第一个
中的5变为第三个
中的2,则时间为3

如果在4之前没有事件发生,则事件为0,时间为4

若发生丢失,则事件为0,时间为该时间

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
。。。等等

我试图用
for
if
语句编写代码。但是,它产生了错误

比如说,

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
for(2:4中的i){
if(设为[,i]1,仅使用第一个元素
4:在if(set[,i]>set$crit){:
条件的长度大于1,并且只使用第一个元素
5:在if(set[,i]set$crit){:
条件的长度大于1,并且只使用第一个元素

您能告诉我问题是什么以及如何解决吗?

您可以使用下面的代码获得答案

x <- unique(set$time)
for(i in length(x))

x长度大于1
if(为4列设置[,i],为什么预期只有一个“事件”列。是否需要
rowSums(设置[1:4]
@akrun(很抱歉,在chrome的注释中ctrl+k不起作用)我编写了如下代码,但错误再次出现。您能告诉我问题出在哪里吗?对于(2:4中的I){ifelse(set[,I]它是关于生存分析的“发生率”。生存分析是关于最早事件及其时间。最早事件之后的事件将被审查。
x <- unique(set$time)
for(i in length(x))