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.