user88911 - 1 year ago 87
R Question

# how to get possible combination of index for set of data.frame object in list?

I have data.frame objects in the list, and I did manipulate all possible index of paired object in the grid. Now, I want to get different combination of group of index for given objects. How can I achieve this more easily/efficiently? Does anyone knows any direct way of doing this sort of grouping index for data.frame objects?

# toy data

``````foo <- data.frame( start=seq(1, by=4, len=6), stop=seq(3, by=4, len=6))
bar <- data.frame(start=seq(5, by=2, len=7), stop=seq(7, by=2, len=7))
bleh <- data.frame(start=seq(1, by=5, len=5), stop=seq(3, by=5, len=5))

myList <- list(foo, bar, bleh)
``````

# using expand.grid, I get as follows:

``````nn <- length(myList)
index <- expand.grid("query"=1:nn, "target"=1:nn)
index <- index[index[,1] <= index[,2],]
``````

# simulated desired output format:

``````\$foo
query    target
1     1         1
4     1         2
7     1         3

\$bar
query    target
4     1         2
5     2         2
8     2         3

\$bleh
query    target
7     1         3
8     2         3
9     3         3
``````

upon the desired output, How can I get this? Is there any R packages provides corresponding methods for doing this sort of manipulation? Can anyone propose possible ideas to do this task? Thanks

We could try

``````i1 <- seq_along(myList)
l1 <- lapply(i1, function(i) seq(i))
l2 <- lapply(seq_along(i1), function(i) i1[!i1 %in% sequence(i-1)] )
Map(function(x,y) {x1 <- expand.grid(x,y)
x1[c(TRUE, diff(x1[,1])>=0),]}, l1, l2)
#[[1]]
#  Var1 Var2
#1    1    1
#2    1    2
#3    1    3

#[[2]]
#  Var1 Var2
#1    1    2
#2    2    2
#4    2    3

#[[3]]
#  Var1 Var2
#1    1    3
#2    2    3
#3    3    3
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download