Hi Eric,
Your solution looks very close to my own. I also use expand.grid to get
the cross product though. If I find an alternate way, I will send out my
solution.
Thanks a lot for your suggestion !
Regards,
Saravanan
On 06/15/2011 09:34 AM, Eric Lecoutre wrote:
> Ho Saravana,
> I did have nearly the same issue some months ago -- you will find
> below a good starting point.
> I am quite sure there is a better way to achieve it or with better
> code, but this should work!
> Kind regards,
>
> Eric
> ---------
> generateCube <- function(x,simplify=TRUE,onlyComb=FALSE,...){
> stopifnot(require(gtools))
> xdf=as.data.frame(x)
> out=vector(length=ncol(xdf),mode="list")
> for (i in 1:ncol(xdf)){ # dimensions of the cube
> icomb <- combinations(length(colnames(xdf)),i,colnames(xdf))
> out[[i]] <- vector(length=nrow(icomb),mode="list")
> for (j in 1:nrow(icomb)){
> ijargs <- icomb[j,]
> names(out[[i]])[j] <- paste(ijargs,collapse="*")
> tmp <-
> with(
> xdf,
> eval(parse(text=
> paste("table(",paste(ijargs,collapse=","),")",sep="")
> ))
> )
> if (simplify & i>1) tmp<-as.data.frame(ftable(tmp))
> out[[i]][[j]] <- tmp
> }
> }
> return(out)
> }
> M=diag(3)
> M[1,2]=2
> M <- rbind(M,M[1,])
> colnames(M) <- paste("V",1:ncol(M),sep="")
> out=generateCube(M)
> out2=generateCube(M,simplify=FALSE)
>
> out[[2]][["V1*V3"]]
> out2[[2]][["V1*V3"]]["0","1"]
> -------------
>
>
> On 14 June 2011 14:59, Saravanan
> <[email protected]
> <mailto:[email protected]>> wrote:
>
> Hello All,
>
> I have a dataset and I wish to obtain all possible data cuboids
> from it using R . For eg if my data frame is :
>
> A B C
> 1 1 1
> 1 2 1
> 2 2 1
>
> The output intended is :
> A=1
> A=2
> B=1
> B=2
> C=1
> A=1,B=1
> A=1,B=2
> A=2,B=2
> A=1,C=1
> A=2,C=1
> B=1,C=1
> B=2,C=1
> A=1,B=1,C=1
> A=1,B=2,C=1
> A=2,B=2,C=1
>
> Are there any function(s) to do this in R ? I tried a combination
> of expand.grid and combn but the resulting code was very ugly and
> needed lot of hacks to make it work. I also tried to check the
> code for arules (which constructs similar "itemsets") but
> unfortunately its code is in C and I am not very familiar in
> writing R extensions. Any pointers to functions will be much
> appreciated.
>
> Regards,
> Saravanan
>
> ______________________________________________
> [email protected] <mailto:[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
> <http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>
> --
> Eric Lecoutre
> Consultant - Business & Decision
> Business Intelligence & Customer Intelligence
[[alternative HTML version deleted]]
______________________________________________
[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.