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: [email protected] [mailto:r-help-bounces@r- > project.org] On Behalf Of brunosm > Sent: Friday 12 October 2012 11:51 > To: [email protected] > 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. > > ______________________________________________ > [email protected] 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. ______________________________________________ [email protected] 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.

