mardi 20 juillet 2021

How to create new variables based on multiple conditions on existing variable values

I have a dataframe with 3 variables:

 P1 P2 P3 
  2 12  8 
  2  1  6 
  2  7  8 
 NA NA  2 
  1 NA  6 
 NA  8 11 

I need to calculate the values ​​for 5 other new variables based on the following conditions:

P1<-c(2,2,2,NA,1,NA)
P2<-c(12,1,7,NA,NA,8)
P3<-c(8,6,8,2,6,11)

df<-data.frame(P1,P2,P3)
attach(df)

X1<-X2<-X3<-X4<-X5<-c()

for(i in 1:(dim(df)[1])){
  #X1
  if((P3[i]==1||P3[i]==2||P3[i]==4||P3[i]==5||P3[i]==7||P3[i]==13) || ((P3[i]==3||P3[i]==6||P3[i]==8||P3[i]==12)&&(P1[i]==1))){X1[i]<-1
  }else{X1[i]<-0}
  
  #X2
  if((P3[i]==3||P3[i]==6||P3[i]==8||P3[i]==11||P3[i]==12)&&
     (P2[i]==1||P2[i]==2||P2[i]==3||P2[i]==5||P2[i]==6||P2[i]==7||P2[i]==10||P2[i]==11)){X2[i]<-1
  }else{X2[i]<-0}
  
  #X3
  if(P3[i]==1||P3[i]==2||P3[i]==4||P3[i]==5||P3[i]==7||P3[i]==13){X3<-1
  }else{X3[i]<-0}
  
  #X4
  if((P3[i]==3||P3[i]==6||P3[i]==8||P3[i]==12)&&(P1[i]==1)){X4<-1
  }else{X4[i]<-0}
  
  #X5
  if((P3[i]==6||P3[i]==8||P3[i]==11) && (P2[i]=12||P2[i]==8)){X5[i]<-1
  }else{X5[i]<-0}
}
df<-cbind(df,X1,X2,X3,X4,X5) 

This is the result I want:

 P1 P2 P3 X1 X2 X3 X4 X5 
  2 12  8  0  0  0  0  1 
  2  1  6  0  1  0  0  0 
  2  7  8  0  1  0  0  0 
 NA NA  2  1  0  1  0  0 
  1 NA  6  1  0  0  1  0 
 NA  8 11  0  0  0  0  1 

But instead of getting it, I get the following error:

Error in if ((P3[i] == 3 || P3[i] == 6 || P3[i] == 8 || P3[i] == 11 || : missing value where TRUE/FALSE needed

I am aware that I have a problem with NAs, as when I remove observations with NA, I don't get that error, but as much as I try different methods, I can't find any way to fix it.

On the other hand, there is an error in the result when I run the rows without NA.

P1<-c( 2,2,2)
P2<-c(12,1,7)  #rows without NA
P3<-c( 8,6,8)
 P1 P2 P3 X1 X2 X3 X4 X5
  2 12  8  0  0  0  0  1
  2  1  6  0  1  0  0  1
  2  7  8  0  1  0  0  1

I get the above result while I should get this:

 P1 P2 P3 X1 X2 X3 X4 X5 
  2 12  8  0  0  0  0  1 
  2  1  6  0  1  0  0  0 
  2  7  8  0  1  0  0  0 

Could someone help me fix this?

Aucun commentaire:

Enregistrer un commentaire