Resolved: Flip/switch values in two columns if value in a third column is positive or negative 0 By Isaac Tonny on 17/06/2022 Issue Share Facebook Twitter LinkedIn Question: I would like to swap/switch the values in two columns if a value in a third column is negative. id <- c("1", "2", "3", "4", "5") effect <- c("A", "C", "G", "A", "G") non_effect <- c("C", "A", "T", "C", "C") variable1 <- c("X", "X", "X", "X", "X") variable2 <- c("Y", "Y", "Y", "Y", "Y") value <-c("0.2", "-0.1", "-0.3", "0.5", "0.8") df <- data.frame(id,effect,non_effect,variable1,variable2,value) [/code] I would get this as a data.frame [code]id effect non_effect variable1 variable2 value 1 A C X Y 0.2 2 C A X Y -0.1 3 G T X Y -0.3 4 A C X Y 0.5 5 G C X Y 0.8 what I want is: id effect non_effect variable1 variable2 value 1 A C X Y 0.2 2 A C X Y 0.1 3 T G X Y 0.3 4 A C X Y 0.5 5 G C X Y 0.8 Answer: df %>% transform(effect = ifelse(value < 0, non_effect, effect), non_effect = ifelse(value < 0, effect, non_effect), value = abs(as.numeric(value))) # A tibble: 5 x 6 id effect non_effect variable1 variable2 value 1 1 A C X Y 0.2 2 2 A C X Y 0.1 3 3 T G X Y 0.3 4 4 A C X Y 0.5 5 5 G C X Y 0.8 If you have better answer, please add a comment about this, thank you! dataframe r
Resolved: How to make statement case insensitive when uploading a dta file with specified columns?27/03/2023