analog_kid
analog_kid

Reputation: 11

R table-adding columns based on condition

The other solution marked as duplicate gave me an error when I tried it on my dataset which has categorical data as well.

I have a table with several columns. One column, column A, has 0, 1, 2, 3, 4 as values. These are codes for a certain condition. I'm trying to create/add another column, column Z, to the table which has 0 if the value in column A is 0 and 1 if the value in column A is 3 or 4. I'm trying to do it via this:

for (i in 1:nrow(pheno_table))
    if pheno_table$columnA == 0
     then pheno_table$newcolumnZ<-0
    elsif pheno_table$columnA == 3 | pheno_table$columnA == 4
     then pheno_table$newcolumnZ<-0

thanks so much @see24! also, I did try this and set the working directory and such but am not able to see the file in the folder (I checked the paths)

    setwd('/pathtofolder/') 

    library(dplyr) df <- data.frame(A=  
    (originaltablefile$column_of_interest)) 
    newcolumn <- df %>% mutate
    (newcolumn = case_when(A == 0 ~ 0, A %in% c(3,4) ~ 1, 
    TRUE ~ NA_real_)) 
    finaltablefile <- cbind(originaltablefile,newcolumn)` 

not able to see finaltablefile in my folder.

Upvotes: 0

Views: 636

Answers (1)

see24
see24

Reputation: 1230

I like to use the mutate and case_when functions from the dplyr package

library(dplyr)
df <- data.frame(A = c(1,2,3,4,0),B = c(3,4,5,6,7))
df2 <- df %>% mutate(Z = case_when(A == 0 ~ 0,
                            A %in% c(3,4) ~ 1,
                            TRUE ~ NA_real_))

I'm assuming that you want NA for rows that are not 1, 3, or 4. The TRUE part means if none of the above are true then... You have to use NA_real_ because case_when requires all the outputs to be of the same type

Upvotes: 1

Related Questions