stefan.petersson at inizio.se
2008-Oct-20 14:10 UTC
[R] Combining all possible values of variables into a new...
I'm trying to create a new column in my data.frame where subjects are categorized depending on values on four other columns. In any other case I would just nest a few ifelse statements, however, in this case i have 4*6*2*3=144 combinations and i get weird 'context overflow' errors. So I wonder if there is a more efficient way of doing this. For illustrational purposes, let's say i have: x<-c(1,0,0,1,0,0,1,0,0,1) y<-c(1,3,2,3,2,1,2,3,2,3) z<-c(1,2,1,2,1,2,1,2,1,2) d<-as.data.frame(cbind(x,y,z)) and i do: d$myvar <- ifelse(d$x == 0 & d$y==1 & d$z==1 , d$myvar <- 1, ifelse(d$x == 0 & d$y==1 & d$z==2 , d$myvar <- 2, ifelse(d$x == 0 & d$y==2 & d$z==1 , d$myvar <- 3, ifelse(d$x == 0 & d$y==2 & d$z==2 , d$myvar <- 4, ifelse(d$x == 0 & d$y==3 & d$z==1 , d$myvar <- 5, ifelse(d$x == 0 & d$y==3 & d$z==2 , d$myvar <- 6, ifelse(d$x == 1 & d$y==1 & d$z==1 , d$myvar <- 7, ifelse(d$x == 1 & d$y==1 & d$z==2 , d$myvar <- 8, ifelse(d$x == 1 & d$y==2 & d$z==1 , d$myvar <- 9, ifelse(d$x == 1 & d$y==2 & d$z==2 , d$myvar <- 10, ifelse(d$x == 1 & d$y==3 & d$z==1 , d$myvar <- 11, ifelse(d$x == 1 & d$y==3 & d$z==2 , d$myvar <- 12, NA)))))))))))) Suggestions?
Dimitris Rizopoulos
2008-Oct-20 14:22 UTC
[R] Combining all possible values of variables into a new...
try this: x <- c(1,0,0,1,0,0,1,0,0,1) y <- c(1,3,2,3,2,1,2,3,2,3) z <- c(1,2,1,2,1,2,1,2,1,2) d <- data.frame(x, y, z) ind <- do.call("paste", c(expand.grid(1:2, 1:3, 0:1)[3:1], sep = "\r")) trg <- do.call("paste", c(d, sep = "\r")) d$myvar <- match(trg, ind) I hope it helps. Best, Dimitris stefan.petersson at inizio.se wrote:> I'm trying to create a new column in my data.frame where subjects are categorized depending on values on four other columns. In any other case I would just nest a few ifelse statements, however, in this case i have 4*6*2*3=144 combinations and i get weird 'context overflow' errors. So I wonder if there is a more efficient way of doing this. > > For illustrational purposes, let's say i have: > > x<-c(1,0,0,1,0,0,1,0,0,1) > y<-c(1,3,2,3,2,1,2,3,2,3) > z<-c(1,2,1,2,1,2,1,2,1,2) > d<-as.data.frame(cbind(x,y,z)) > > and i do: > > d$myvar <- ifelse(d$x == 0 & d$y==1 & d$z==1 , d$myvar <- 1, > ifelse(d$x == 0 & d$y==1 & d$z==2 , d$myvar <- 2, > ifelse(d$x == 0 & d$y==2 & d$z==1 , d$myvar <- 3, > ifelse(d$x == 0 & d$y==2 & d$z==2 , d$myvar <- 4, > ifelse(d$x == 0 & d$y==3 & d$z==1 , d$myvar <- 5, > ifelse(d$x == 0 & d$y==3 & d$z==2 , d$myvar <- 6, > ifelse(d$x == 1 & d$y==1 & d$z==1 , d$myvar <- 7, > ifelse(d$x == 1 & d$y==1 & d$z==2 , d$myvar <- 8, > ifelse(d$x == 1 & d$y==2 & d$z==1 , d$myvar <- 9, > ifelse(d$x == 1 & d$y==2 & d$z==2 , d$myvar <- 10, > ifelse(d$x == 1 & d$y==3 & d$z==1 , d$myvar <- 11, > ifelse(d$x == 1 & d$y==3 & d$z==2 , d$myvar <- 12, NA)))))))))))) > > Suggestions? > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Gustaf Rydevik
2008-Oct-20 14:32 UTC
[R] Combining all possible values of variables into a new...
On Mon, Oct 20, 2008 at 4:10 PM, <stefan.petersson at inizio.se> wrote:> > I'm trying to create a new column in my data.frame where subjects are categorized depending on values on four other columns. In any other case I would just nest a few ifelse statements, however, in this case i have 4*6*2*3=144 combinations and i get weird 'context overflow' errors. So I wonder if there is a more efficient way of doing this. > > For illustrational purposes, let's say i have: > > x<-c(1,0,0,1,0,0,1,0,0,1) > y<-c(1,3,2,3,2,1,2,3,2,3) > z<-c(1,2,1,2,1,2,1,2,1,2) > d<-as.data.frame(cbind(x,y,z)) > > and i do: > > d$myvar <- ifelse(d$x == 0 & d$y==1 & d$z==1 , d$myvar <- 1, > ifelse(d$x == 0 & d$y==1 & d$z==2 , d$myvar <- 2, > ifelse(d$x == 0 & d$y==2 & d$z==1 , d$myvar <- 3, > ifelse(d$x == 0 & d$y==2 & d$z==2 , d$myvar <- 4, > ifelse(d$x == 0 & d$y==3 & d$z==1 , d$myvar <- 5, > ifelse(d$x == 0 & d$y==3 & d$z==2 , d$myvar <- 6, > ifelse(d$x == 1 & d$y==1 & d$z==1 , d$myvar <- 7, > ifelse(d$x == 1 & d$y==1 & d$z==2 , d$myvar <- 8, > ifelse(d$x == 1 & d$y==2 & d$z==1 , d$myvar <- 9, > ifelse(d$x == 1 & d$y==2 & d$z==2 , d$myvar <- 10, > ifelse(d$x == 1 & d$y==3 & d$z==1 , d$myvar <- 11, > ifelse(d$x == 1 & d$y==3 & d$z==2 , d$myvar <- 12, NA)))))))))))) > > Suggestions?How about the following? x<-c(1,0,0,1,0,0,1,0,0,1) y<-c(1,3,2,3,2,1,2,3,2,3) z<-c(1,2,1,2,1,2,1,2,1,2) d<-as.data.frame(cbind(x,y,z)) xyz.comb<-interaction(x,y,z,lex.order=T) d$myvar<-match(xyz.comb,levels(xyz.comb)) /Gustaf Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik
David Hajage
2008-Oct-20 14:33 UTC
[R] Combining all possible values of variables into a new...
Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible URL : <https://stat.ethz.ch/pipermail/r-help/attachments/20081020/74e8107a/attachment.pl>
Petr PIKAL
2008-Oct-20 14:39 UTC
[R] Odp: Combining all possible values of variables into a new...
Hi maybe you can change combination of your columns to factor new <-factor(paste(x,y,z, sep="")) and then reorder this factor (see reorder) change it to numeric to get index index <- as.numeric(factor) and then choose from set of 144 data data[index] or use your index directly if you need just numbers from 1 to the final to get your myvar. Maybe you could also look to permutations function from gtools package Regards Petr r-help-bounces at r-project.org napsal dne 20.10.2008 16:10:26:> > I'm trying to create a new column in my data.frame where subjects are > categorized depending on values on four other columns. In any other caseI> would just nest a few ifelse statements, however, in this case i have > 4*6*2*3=144 combinations and i get weird 'context overflow' errors. So I> wonder if there is a more efficient way of doing this. > > For illustrational purposes, let's say i have: > > x<-c(1,0,0,1,0,0,1,0,0,1) > y<-c(1,3,2,3,2,1,2,3,2,3) > z<-c(1,2,1,2,1,2,1,2,1,2) > d<-as.data.frame(cbind(x,y,z)) > > and i do: > > d$myvar <- ifelse(d$x == 0 & d$y==1 & d$z==1 , d$myvar <- 1, > ifelse(d$x == 0 & d$y==1 & d$z==2 , d$myvar <- 2, > ifelse(d$x == 0 & d$y==2 & d$z==1 , d$myvar <- 3, > ifelse(d$x == 0 & d$y==2 & d$z==2 , d$myvar <- 4, > ifelse(d$x == 0 & d$y==3 & d$z==1 , d$myvar <- 5, > ifelse(d$x == 0 & d$y==3 & d$z==2 , d$myvar <- 6, > ifelse(d$x == 1 & d$y==1 & d$z==1 , d$myvar <- 7, > ifelse(d$x == 1 & d$y==1 & d$z==2 , d$myvar <- 8, > ifelse(d$x == 1 & d$y==2 & d$z==1 , d$myvar <- 9, > ifelse(d$x == 1 & d$y==2 & d$z==2 , d$myvar <- 10, > ifelse(d$x == 1 & d$y==3 & d$z==1 , d$myvar <- 11, > ifelse(d$x == 1 & d$y==3 & d$z==2 , d$myvar <- 12, NA)))))))))))) > > Suggestions? > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
jim holtman
2008-Oct-20 16:14 UTC
[R] Combining all possible values of variables into a new...
yet another way (as it always is in R):> x<-c(1,0,0,1,0,0,1,0,0,1) > y<-c(1,3,2,3,2,1,2,3,2,3) > z<-c(1,2,1,2,1,2,1,2,1,2) > d<-as.data.frame(cbind(x,y,z)) > d$myvar <- x*6 + (y-1)*2 + z > > dx y z myvar 1 1 1 1 7 2 0 3 2 6 3 0 2 1 3 4 1 3 2 12 5 0 2 1 3 6 0 1 2 2 7 1 2 1 9 8 0 3 2 6 9 0 2 1 3 10 1 3 2 12 On Mon, Oct 20, 2008 at 10:10 AM, <stefan.petersson at inizio.se> wrote:> > I'm trying to create a new column in my data.frame where subjects are categorized depending on values on four other columns. In any other case I would just nest a few ifelse statements, however, in this case i have 4*6*2*3=144 combinations and i get weird 'context overflow' errors. So I wonder if there is a more efficient way of doing this. > > For illustrational purposes, let's say i have: > > x<-c(1,0,0,1,0,0,1,0,0,1) > y<-c(1,3,2,3,2,1,2,3,2,3) > z<-c(1,2,1,2,1,2,1,2,1,2) > d<-as.data.frame(cbind(x,y,z)) > > and i do: > > d$myvar <- ifelse(d$x == 0 & d$y==1 & d$z==1 , d$myvar <- 1, > ifelse(d$x == 0 & d$y==1 & d$z==2 , d$myvar <- 2, > ifelse(d$x == 0 & d$y==2 & d$z==1 , d$myvar <- 3, > ifelse(d$x == 0 & d$y==2 & d$z==2 , d$myvar <- 4, > ifelse(d$x == 0 & d$y==3 & d$z==1 , d$myvar <- 5, > ifelse(d$x == 0 & d$y==3 & d$z==2 , d$myvar <- 6, > ifelse(d$x == 1 & d$y==1 & d$z==1 , d$myvar <- 7, > ifelse(d$x == 1 & d$y==1 & d$z==2 , d$myvar <- 8, > ifelse(d$x == 1 & d$y==2 & d$z==1 , d$myvar <- 9, > ifelse(d$x == 1 & d$y==2 & d$z==2 , d$myvar <- 10, > ifelse(d$x == 1 & d$y==3 & d$z==1 , d$myvar <- 11, > ifelse(d$x == 1 & d$y==3 & d$z==2 , d$myvar <- 12, NA)))))))))))) > > Suggestions? > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?