jeudi 22 juin 2017

In R how to add a new column of label that specifies a groupd of people?

my data is about patients features and their disease stage, recorded at different times, like below: they are sorted by patient ID, and different rows means tests at different times.

enter image description here

I want to add a new column of label: for those whose "ckd0" feature is 0 at their last test, label them as 1; for those whose "ckd0" feature is 1 at their last test, label them as 0.

I tried the code as below, but has error: Error in if (temp$ckd0[b] == 0) { : argument is of length zero

Can any body tell me why and how to fix it? x=unique(personal_id) label = vector(mode = "numeric",length = 0)

for (i in 1:length(x)){
  temp=subset(mydata_1[which(mydata_1$personal_id==x[i]),])
  k=numeric(length(temp))
  b=order(temp$report_time)[length(temp$report_time)]

  if (temp$ckd0[b] == 0){
    k[]=1
  }
  label=c(label,k)
}

nonckd <- cbind(mydata_1,label) 

Aucun commentaire:

Enregistrer un commentaire