[R] combination which limited

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jun 13 14:38:10 CEST 2005


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 = dd2

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))




More information about the R-help mailing list