Hi, i'm trying to simplify some R code but i got stucked in this: test<-data.frame(cbind(id,x1,x2,x3,x4,x5,x6,x7)) test> testid x1 x2 x3 x4 x5 x6 x7 1 1 36 26 21 32 31 27 31 2 2 45 21 46 50 22 36 29 3 3 49 47 35 44 33 31 46 4 4 42 32 38 28 39 45 32 5 5 29 42 39 48 25 35 34 6 6 39 31 30 37 46 43 44 7 7 41 40 25 23 42 40 24 8 8 27 29 47 34 26 38 28 9 9 25 35 29 36 43 34 23 10 10 24 44 37 26 27 46 22 11 11 38 50 32 49 37 24 40 12 12 20 34 48 25 30 41 36 13 13 26 46 20 40 29 20 43 14 14 33 37 49 31 47 30 30 15 15 43 39 27 35 48 47 27 count40<-ifelse(test$x1==40|test$x2==40|test$x3==40|test$x4==40|test$x5==40|test$x6==40|test$x7==40,0,1) count40 I'm trying to remake that ifelse. If any variable from x1 to x7 equals to 40 --> 0, else --> 1 I was trying to do something like: count40aux<-ifelse(test[,2:8]==40,0,1) count40aux It doesn't work as i expected... Can anyone help me please? Regards, Bruno -- View this message in context: http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981.html Sent from the R help mailing list archive at Nabble.com.
That was exacly what i was looking for! Thanks a lot! Cheers! -- View this message in context: http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4645990.html Sent from the R help mailing list archive at Nabble.com.
You can use subscripting on the matrix, e.g. ind <- test == 40 and the test[ind] <- 1. Just deal with the "id" column when you set the rest to 0.> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of brunosm > Sent: Friday 12 October 2012 11:51 > To: r-help at r-project.org > Subject: [R] ifelse reformulation > > Hi, i'm trying to simplify some R code but i got stucked in this: > > test<-data.frame(cbind(id,x1,x2,x3,x4,x5,x6,x7)) > test > > > test > id x1 x2 x3 x4 x5 x6 x7 > 1 1 36 26 21 32 31 27 31 > 2 2 45 21 46 50 22 36 29 > 3 3 49 47 35 44 33 31 46 > 4 4 42 32 38 28 39 45 32 > 5 5 29 42 39 48 25 35 34 > 6 6 39 31 30 37 46 43 44 > 7 7 41 40 25 23 42 40 24 > 8 8 27 29 47 34 26 38 28 > 9 9 25 35 29 36 43 34 23 > 10 10 24 44 37 26 27 46 22 > 11 11 38 50 32 49 37 24 40 > 12 12 20 34 48 25 30 41 36 > 13 13 26 46 20 40 29 20 43 > 14 14 33 37 49 31 47 30 30 > 15 15 43 39 27 35 48 47 27 > > count40<- > ifelse(test$x1==40|test$x2==40|test$x3==40|test$x4==40|test$x5==40|test > $x6==40|test$x7==40,0,1) > count40 > > I'm trying to remake that ifelse. If any variable from x1 to x7 equals > to 40 > --> 0, else --> 1 > > I was trying to do something like: > > count40aux<-ifelse(test[,2:8]==40,0,1) > count40aux > > It doesn't work as i expected... > > Can anyone help me please? > > Regards, > > Bruno > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/ifelse- > reformulation-tp4645981.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Hi arun kirshna just let me ask you something else. Imagin that i only want to "search" in the variables x3,x4 and x5. How can i do this? Regards, Bruno -- View this message in context: http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4646013.html Sent from the R help mailing list archive at Nabble.com.
HI, Try this: count40<- ifelse(test1$x3==40|test1$x4==40|test1$x5==40,0,1) count40 # [1] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 as.vector(apply(test1,1,function(x) ifelse(any(x[4:6]==40),0,1))) # [1] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 A.K. ----- Original Message ----- From: brunosm <brunosm87 at gmail.com> To: r-help at r-project.org Cc: Sent: Friday, October 12, 2012 10:18 AM Subject: Re: [R] ifelse reformulation That was exacly what i was looking for! Thanks a lot! Cheers! -- View this message in context: http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4645990.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list 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.
Hi, Try this: test1<-read.table(text=" ? ?id x1 x2 x3 x4 x5 x6 x7 1 ? 1 36 26 21 32 31 27 31 2 ? 2 45 21 46 50 22 36 29 3 ? 3 49 47 35 44 33 31 46 4 ? 4 42 32 38 28 39 45 32 5 ? 5 29 42 39 48 25 35 34 6 ? 6 39 31 30 37 46 43 44 7 ? 7 41 40 25 23 42 40 24 8 ? 8 27 29 47 34 26 38 28 9 ? 9 25 35 29 36 43 34 23 10 10 24 44 37 26 27 46 22 11 11 38 50 32 49 37 24 40 12 12 20 34 48 25 30 41 36 13 13 26 46 20 40 29 20 43 14 14 33 37 49 31 47 30 30 15 15 43 39 27 35 48 47 27 ",sep="",header=TRUE) group<-c(40,50,60,70) as.vector(apply(test1,1,function(x) ifelse(any(x[4:6]%in%group),0,1))) # [1] 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 #Comparing the results individually ifelse(test1$x4==40|test1$x4==50|test1$x4==60|test1$x4==70|test1$x5==40|test1$x5==50|test1$x5==60|test1$x5==70|test1$x6==40|test1$x6==50|test1$x6==60|test1$x7==70,0,1) # [1] 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1 ?as.vector(apply(test1,1,function(x) ifelse(any(x[4:6]==40|x[4:6]==50|x[4:6]==60|x[4:6]==70),0,1))) ?#[1] 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 A.K. ________________________________ From: Bruno Marques <brunosm87 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Wednesday, October 24, 2012 12:47 PM Subject: Re: [R] ifelse reformulation Hi Arun, let me ask you just one more thing, please. If i want to compare, not one but a group of values, can i do this: group<-c(40,50,60,70) as.vector(apply(test1,1,function(x) ifelse(any(x[4:6]==group),0,1))) What i want is: IF it finds any of the values in group, then 0. I mean, it has to work like a OR... but i think this code will work like an AND... am i wrong? Sorry for my bad english! Best regards, Bruno 2012/10/12 arun <smartpink111 at yahoo.com> HI,>Try this: >count40<- ifelse(test1$x3==40|test1$x4==40|test1$x5==40,0,1) >count40 > ># [1] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 >as.vector(apply(test1,1,function(x) ifelse(any(x[4:6]==40),0,1))) ># [1] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 >A.K. > > > > >----- Original Message ----- >From: brunosm <brunosm87 at gmail.com> >To: r-help at r-project.org >Cc: >Sent: Friday, October 12, 2012 10:18 AM >Subject: Re: [R] ifelse reformulation > >That was exacly what i was looking for! Thanks a lot! >Cheers! > > > >-- >View this message in context: http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4645990.html > >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >R-help at r-project.org mailing list >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. > >
Arun, thank you very much... but a new problem arose... What if... in the variable group that i want to compare, there is numeric and non numeric types? Or, if you think its better, can i have two variables, one numeric and one non numeric, and made the comparision? Best regards, Bruno 2012/10/12 arun kirshna [via R] <ml-node+s789695n4645988h70@n4.nabble.com>> HI, > Try this: > test1<-read.table(text=" > id x1 x2 x3 x4 x5 x6 x7 > 1 1 36 26 21 32 31 27 31 > 2 2 45 21 46 50 22 36 29 > 3 3 49 47 35 44 33 31 46 > 4 4 42 32 38 28 39 45 32 > 5 5 29 42 39 48 25 35 34 > 6 6 39 31 30 37 46 43 44 > 7 7 41 40 25 23 42 40 24 > 8 8 27 29 47 34 26 38 28 > 9 9 25 35 29 36 43 34 23 > 10 10 24 44 37 26 27 46 22 > 11 11 38 50 32 49 37 24 40 > 12 12 20 34 48 25 30 41 36 > 13 13 26 46 20 40 29 20 43 > 14 14 33 37 49 31 47 30 30 > 15 15 43 39 27 35 48 47 27 > ",sep="",header=TRUE) > count40<- > ifelse(test1$x1==40|test1$x2==40|test1$x3==40|test1$x4==40|test1$x5==40|test1$x6==40|test1$x7==40,0,1) > > count40 > #[1] 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 > #if you want to get the same result, > apply(test1,1,function(x) ifelse(any(x==40),0,1)) > # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 > #1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 > A.K. > > > > > > ------------------------------ > If you reply to this email, your message will be added to the discussion > below: > http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4645988.html > To unsubscribe from ifelse reformulation, click here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4645981&code=YnJ1bm9zbTg3QGdtYWlsLmNvbXw0NjQ1OTgxfDIwMjc4MjE3MDg=> > . > NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> >-- View this message in context: http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4647431.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
Hi, Did you mean this? group1<-c(40,50,"60",70) #or group2<-c(50,"var1","var2",60) In either of the above cases, when you check str(group1) # all are converted to character. # chr [1:4] "40" "50" "60" "70" ?str(group2) # chr [1:4] "50" "var1" "var2" "60" Suppose, I am comparing the test1 dataset (x4: x6 columns) with group2 ?apply(test1,1,function(x) ifelse(x[5:7]%in%group2,0,1)) # 2nd row is 50 for x4 column ?? #? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #[1,] 1 0 1 1 1 1 1 1 1? 1? 1? 1? 1? 1? 1 #[2,] 1 1 1 1 1 1 1 1 1? 1? 1? 1? 1? 1? 1 #[3,] 1 1 1 1 1 1 1 1 1? 1? 1? 1? 1? 1? 1 # final result as.vector(apply(test1,1,function(x) ifelse(any(x[5:7]%in%group2),0,1))) # [1] 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 I guess it works. BTW, in my previous reply, I made a mistake as.vector(apply(test1,1,function(x) ifelse(any(x[4:6]%in%group),0,1)))? ?????????????????????????????????????????????????????????????????????? ^^^^^^^ It should be 5:7. group<-c(40,50,60,70) as.vector(apply(test1,1,function(x) ifelse(any(x[5:7]%in%group),0,1))) ?#[1] 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1 ifelse(test1$x4==40|test1$x4==50|test1$x4==60|test1$x4==70|test1$x5==40|test1$x5==50|test1$x5==60|test1$x5==70|test1$x6==40|test1$x6==50|test1$x6==60|test1$x7==70,0,1) # [1] 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1 ?as.vector(apply(test1,1,function(x) ifelse(any(x[5:7]==40|x[5:7]==50|x[5:7]==60|x[5:7]==70),0,1))) ?#[1] 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1 A.K. ----- Original Message ----- From: brunosm <brunosm87 at gmail.com> To: r-help at r-project.org Cc: Sent: Thursday, October 25, 2012 12:55 PM Subject: Re: [R] ifelse reformulation Arun, thank you very much... but a new problem arose... What if... in the variable group that i want to compare, there is numeric and non numeric types? Or, if you think its better, can i have two variables, one numeric and one non numeric, and made the comparision? Best regards, Bruno 2012/10/12 arun kirshna [via R] <ml-node+s789695n4645988h70 at n4.nabble.com>> HI, > Try this: > test1<-read.table(text=" >? ? id x1 x2 x3 x4 x5 x6 x7 > 1? 1 36 26 21 32 31 27 31 > 2? 2 45 21 46 50 22 36 29 > 3? 3 49 47 35 44 33 31 46 > 4? 4 42 32 38 28 39 45 32 > 5? 5 29 42 39 48 25 35 34 > 6? 6 39 31 30 37 46 43 44 > 7? 7 41 40 25 23 42 40 24 > 8? 8 27 29 47 34 26 38 28 > 9? 9 25 35 29 36 43 34 23 > 10 10 24 44 37 26 27 46 22 > 11 11 38 50 32 49 37 24 40 > 12 12 20 34 48 25 30 41 36 > 13 13 26 46 20 40 29 20 43 > 14 14 33 37 49 31 47 30 30 > 15 15 43 39 27 35 48 47 27 > ",sep="",header=TRUE) > count40<- > ifelse(test1$x1==40|test1$x2==40|test1$x3==40|test1$x4==40|test1$x5==40|test1$x6==40|test1$x7==40,0,1) > > count40 >? #[1] 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 > #if you want to get the same result, >? apply(test1,1,function(x) ifelse(any(x==40),0,1)) > # 1? 2? 3? 4? 5? 6? 7? 8? 9 10 11 12 13 14 15 >? #1? 1? 1? 1? 1? 1? 0? 1? 1? 1? 0? 1? 0? 1? 1 > A.K. > > > > > > ------------------------------ >? If you reply to this email, your message will be added to the discussion > below: > http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4645988.html >? To unsubscribe from ifelse reformulation, click here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4645981&code=YnJ1bm9zbTg3QGdtYWlsLmNvbXw0NjQ1OTgxfDIwMjc4MjE3MDg=> > . > NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> >-- View this message in context: http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4647431.html Sent from the R help mailing list archive at Nabble.com. ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.