Hi everyone, I have two columns: ? ?A? ? ? ? ? ? ? ?B ? ?1? ? ? ? ? ? ? ?1? ?1? ? ? ? ? ? ? ?0 ? ?0? ? ? ? ? ? ? ?1 ? ?0? ? ? ? ? ? ? ?0 I have 4 categories which are: 1) if A = 1 and B =1 then A1 = 1, else A2 = 0, A3 = 0, A4 = 0 2) if A = 1 and B =0 then A1 = 0, else A2 =1, A3 = 0, A4 = 0 3) if A = 0 and B = 1 then A1 = 0, else A2 = 0, A3 = 1, A4 = 0 4) if A = 0 and B =0 then A1 = 0, else A2 = 0, A3 = 0, A4 = 1 I want the data become like below: ? ?A? ? ? ? B? ? ? A1? ? ? A2? ? A3? ? ?A4 ? ?1? ? ? ? 1? ? ? ?1????? ? 0????? ? 0? ? ? 0? ?1? ? ? ? 0? ? ? ?0? ? ? ? 1????? ? 0????? 0 ? ?0? ? ? ? 1? ? ? ?0????? ? 0? ? ? ? 1????? 0 ? ?0? ? ? ? 0? ? ? ?0????? ? 0? ? ? ? 0????? 1 Anyone can help me? Many Thanks. Regards, Zuhri | | Virus-free. www.avast.com |
if else with 4 conditions problem
2 messages · smart hendsome, Rui Barradas
Hello,
It's just a sequence of ifelse instructions.
dat <- read.table(text = "
A B
1 1
1 0
0 1
0 0
", header = TRUE)
dat$A1 <- ifelse(dat$A == 1 & dat$B == 1, 1, 0)
dat$A2 <- ifelse(dat$A == 1 & dat$B == 0, 1, 0)
dat$A3 <- ifelse(dat$A == 0 & dat$B == 1, 1, 0)
dat$A4 <- ifelse(dat$A == 0 & dat$B == 0, 1, 0)
dat
Hope this helps,
Rui Barradas
On 5/27/2018 3:13 PM, smart hendsome via R-help wrote:
Hi everyone, I have two columns: ? ?A? ? ? ? ? ? ? ?B ? ?1? ? ? ? ? ? ? ?1? ?1? ? ? ? ? ? ? ?0 ? ?0? ? ? ? ? ? ? ?1 ? ?0? ? ? ? ? ? ? ?0 I have 4 categories which are: 1) if A = 1 and B =1 then A1 = 1, else A2 = 0, A3 = 0, A4 = 0 2) if A = 1 and B =0 then A1 = 0, else A2 =1, A3 = 0, A4 = 0 3) if A = 0 and B = 1 then A1 = 0, else A2 = 0, A3 = 1, A4 = 0 4) if A = 0 and B =0 then A1 = 0, else A2 = 0, A3 = 0, A4 = 1 I want the data become like below: ? ?A? ? ? ? B? ? ? A1? ? ? A2? ? A3? ? ?A4 ? ?1? ? ? ? 1? ? ? ?1????? ? 0????? ? 0? ? ? 0? ?1? ? ? ? 0? ? ? ?0? ? ? ? 1????? ? 0????? 0 ? ?0? ? ? ? 1? ? ? ?0????? ? 0? ? ? ? 1????? 0 ? ?0? ? ? ? 0? ? ? ?0????? ? 0? ? ? ? 0????? 1 Anyone can help me? Many Thanks. Regards, Zuhri | | Virus-free. www.avast.com | [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.