lundi 21 juin 2021

Create a new column with values from other columns in the dataset

I would like to create a column filled with information from either one or other column from my dataset, dependent on a third column.

My dataset is full of 0 and 1 values, looks like this:

df <- data.frame(PatientID = c("0002" ,"0004", "0005", "0006" ,"0009" ,"0010" ,"0018", "0019" ,"0020" ,"0027", "0039" ,"0041" ,"0042", "0043" ,"0044" ,"0045", "0046", "0047" ,"0048" ,"0049", "0055"),
                RCA = c( 1 , 1 , 0 , 1 , 1, 1  ,0 , 0 , 0  ,0,  0 , 0 , 0 , 0  ,1 , 1 , 1 , 0 , 1 , 1  ,1), 
                RCB= c(1 , 1 , 1 , 1 , 0, 0  ,0 , 0 , 0  ,0,  0 , 0 , 0 , 0  ,1 , 0 , 1 , 0 , 1 , 1  ,1),
                RCC = c( 1 , 1 , 1 , 1 , 0, 0  ,0 , 0 , 0  ,1 , 1 , 1  , 0 , 0  ,1 , 1 , 1 , 1  , 1 , 1  ,1), stringsAsFactors = F)

The fourth column df$RCD, would need to follow the following condition:

If df$RCC is 1 then df$RCD= df$RCB, and if df$RCC is 0 then df$RCD= df$RCA

Hope this makes sense. I have attached an example output for clarity.

enter image description here

Thank you so much! Best regards

Aucun commentaire:

Enregistrer un commentaire