mercredi 3 avril 2019

How to create a new column with conditional logic, based on several values found in multiple columns?

I have a data set of birth defects (test), in which each row is a case, with a different 5-way combination of defects. The first five columns of the data set (Defect_A, Defect_B, Defect_C, Defect_D, Defect_E) are the defect numbers that make up this combination.

I want to create a new column called “comments” that outputs a comment based on the following conditional logic:

  1. If a case/row has any of the following defects (1, 2, 3, 4) in columns 1:5, comments = “conjoined”
  2. Ifelse a case has any TWO of the following defects (5, 6, 7, 8) in columns 1:5, comments = “spina bifida”
  3. Ifelse a case has any one of the following defects (5, 6, 7, 8) AND one of the following defects (9,10,11,12,13) in columns 1:5, comments = “heterodaxy”
  4. Ifelse a case has any THREE of the following defects (14,15,16,17,18) in columns 1:5, comments = “vacterl”

 

      Defect_A Defect_B Defect_C Defect_D Defect_E
 [1,]       11        5        3       27       10
 [2,]       20        5       20       17       30
 [3,]        5        8        9        5       11
 [4,]       10        8        1        1       15
 [5,]        3       27       12       24       29
 [6,]       12       18       27       13       25
 [7,]        3       28       27       24        7
 [8,]       15        4       14       10       13
 [9,]       26        3       16       29       22
[10,]       13       14       13       27       11

How would I go about doing this? I’ve included sample code below.

# Sample data set
Defect_A = sample(1:30, 10, replace=TRUE)  
Defect_B = sample(1:30, 10, replace=TRUE)  
Defect_C = sample(1:30, 10, replace=TRUE) 
Defect_D = sample(1:30, 10, replace=TRUE)  
Defect_E = sample(1:30, 10, replace=TRUE)  
test<-cbind(Defect_A, Defect_B, Defect_C, Defect_D, Defect_E)  

# Conditions
any <- c(1,2,3,4) # for condition 1  
any_2 <- c(5,6,7,8) # for conditions 2 and 3  
any_2_plus <- c(9,10,11,12,13) # for condition 3  
any_3 <- c(14,15,16,17,18) # for condition 4  

Aucun commentaire:

Enregistrer un commentaire