Dear R-helpers, I am learning about combination in R. I want to combination all of possible variable but it limited. I am sorry I could not explain exactly. For usefull I give an example interface <- c("usb","fireware","infra","bluetooth") screen <- c("lcd","cube") computer <- c("pc","server","laptop") available <- c("yes","no") What the result I need, something like this below, usb lcd pc yes fireware lcd pc yes infra lcd pc yes bluetooth lcd pc yes usb cube pc yes usb lcd server yes usb lcd laptop yes usb lcd pc no How can I do that? I was wondering if someone can help me. Thanks you for your time and best regards, Muhammad Subianto
On Sat, 2005-06-11 at 20:44 +0200, Muhammad Subianto wrote:> Dear R-helpers, > I am learning about combination in R. > I want to combination all of > possible variable but it limited. > I am sorry I could not explain exactly. > For usefull I give an example > interface <- c("usb","fireware","infra","bluetooth") > screen <- c("lcd","cube") > computer <- c("pc","server","laptop") > available <- c("yes","no") > > What the result I need, something like this below, > usb lcd pc yes > fireware lcd pc yes > infra lcd pc yes > bluetooth lcd pc yes > usb cube pc yes > usb lcd server yes > usb lcd laptop yes > usb lcd pc no > > How can I do that? > I was wondering if someone can help me. > Thanks you for your time and best regards, > Muhammad SubiantoUse:> expand.grid(interface, screen, computer, available)Var1 Var2 Var3 Var4 1 usb lcd pc yes 2 fireware lcd pc yes 3 infra lcd pc yes 4 bluetooth lcd pc yes 5 usb cube pc yes 6 fireware cube pc yes 7 infra cube pc yes 8 bluetooth cube pc yes 9 usb lcd server yes 10 fireware lcd server yes 11 infra lcd server yes 12 bluetooth lcd server yes 13 usb cube server yes 14 fireware cube server yes 15 infra cube server yes 16 bluetooth cube server yes 17 usb lcd laptop yes 18 fireware lcd laptop yes 19 infra lcd laptop yes 20 bluetooth lcd laptop yes 21 usb cube laptop yes 22 fireware cube laptop yes 23 infra cube laptop yes 24 bluetooth cube laptop yes 25 usb lcd pc no 26 fireware lcd pc no 27 infra lcd pc no 28 bluetooth lcd pc no 29 usb cube pc no 30 fireware cube pc no 31 infra cube pc no 32 bluetooth cube pc no 33 usb lcd server no 34 fireware lcd server no 35 infra lcd server no 36 bluetooth lcd server no 37 usb cube server no 38 fireware cube server no 39 infra cube server no 40 bluetooth cube server no 41 usb lcd laptop no 42 fireware lcd laptop no 43 infra lcd laptop no 44 bluetooth lcd laptop no 45 usb cube laptop no 46 fireware cube laptop no 47 infra cube laptop no 48 bluetooth cube laptop no See ?expand.grid for more information. Using:> help.search("combinations")would guide you to that function based upon a keyword search. HTH, Marc Schwartz
On 6/13/05, Muhammad Subianto <subianto at gmail.com> wrote:> Dear R-helpers, > > On this day 6/12/2005 10:48 AM, Muhammad Subianto wrote: > > Dear All, > > Many thanks to Marc Schwartz and Gabor Grothendieck who have explained > > me about using expand.grid function and clearly explain how to use > > JGR. > > > > > >>dd <- expand.grid(interface = interface, screen = screen, > >> computer = computer, available = available) > >> > >>There are several possibilities now: > >> > >>1. you could list out dd on the console and note the number of the > >>rows you want to keep: > >> > >>idx <- c(1,5,7) > >>dd2 <- dd[,idx] > >> > > > > > > I like a possible no. 1, because I can use and explore with my hand, > > > >> idx <- c(1:5,9,17,25) > >> dd2 <- dd[idx,] > >> dd2 > > > > interface screen computer available > > 1 usb lcd pc yes > > 2 fireware lcd pc yes > > 3 infra lcd pc yes > > 4 bluetooth lcd pc yes > > 5 usb cube pc yes > > 9 usb lcd server yes > > 17 usb lcd laptop yes > > 25 usb lcd pc no > > > > > > Regards, > > Muhammad Subianto > > Notepad, Copy and Paste are my best friend to use R.2.1.0 on windows 2000 > > > > As previous mail, using expand.grid can handle all variables in > datasets. But, if I need only one or more combinations I can choice > combination (rows) which I need, > > interface <- c("usb","fireware","infra","bluetooth") > screen <- c("lcd","cube") > computer <- c("pc","server","laptop") > available <- c("yes","no") > dd <- > expand.grid(interface=interface,screen=screen,computer=computer,available=available) > idx <- c(1:5,9,17,25) # this combination rows) what I need > dd2 <- dd[idx,] > dd2 > > Because I only need combination (1,2,3,4,5,9,17 and 25), I tried to make > a simple code to make sure what pattern the combination I have. I was > wondering if someone can help me to make a simple function. > > smdxc <- rbind( > c(levels(dd[,1])[1], #combination 1 > levels(dd[,2])[1], > levels(dd[,3])[1], > levels(dd[,4])[1]), > > c(levels(dd[,1])[2], #combination 2 > levels(dd[,2])[1], > levels(dd[,3])[1], > levels(dd[,4])[1]), > > c(levels(dd[,1])[3], #combination 3 > levels(dd[,2])[1], > levels(dd[,3])[1], > levels(dd[,4])[1]), > > c(levels(dd[,1])[4], #combination 4 > levels(dd[,2])[1], > levels(dd[,3])[1], > levels(dd[,4])[1]), > > c(levels(dd[,1])[1], #combination 5 > levels(dd[,2])[2], > levels(dd[,3])[1], > levels(dd[,4])[1]), > > c(levels(dd[,1])[1], #combination 9 > levels(dd[,2])[1], > levels(dd[,3])[2], > levels(dd[,4])[1]), > > c(levels(dd[,1])[1], #combination 17 > levels(dd[,2])[1], > levels(dd[,3])[3], > levels(dd[,4])[1]), > > c(levels(dd[,1])[1], #combination 25 > levels(dd[,2])[1], > levels(dd[,3])[1], > levels(dd[,4])[2])) > > smdxc # the result = dd2The pattern seems to be that each row contains at most one column that is not at level 1. That is the entry at row i and column col[i] has level lev[i] and all other entries are at level 1. col <- c(1,1,1,1,2,3,3,4) lev <- c(1:4,2,2,3,2) mat <- matrix(1, length(col), 4) mat[cbind(seq(col),col)] <- lev data.frame(interface = factor(mat[,1], lab = interface), screen = factor(mat[,2], lab = screen), computer = factor(mat[,3], lab = computer), available = factor(mat[,4], lab = available))
Dear all, Again, I would like to thank Gabor Grothendieck for your help. I can improve which you suggest with the others combination. And thank you for your time. Sincerely, Muhammad Subianto On this day 6/13/2005 2:38 PM, Gabor Grothendieck wrote:> > The pattern seems to be that each row contains at most one column > that is not at level 1. That is the entry at row i and column col[i] has > level lev[i] and all other entries are at level 1. > > col <- c(1,1,1,1,2,3,3,4) > lev <- c(1:4,2,2,3,2) > mat <- matrix(1, length(col), 4) > mat[cbind(seq(col),col)] <- lev > data.frame(interface = factor(mat[,1], lab = interface), > screen = factor(mat[,2], lab = screen), > computer = factor(mat[,3], lab = computer), > available = factor(mat[,4], lab = available)) > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >