2016-03-24 69 views
0

我已经生成了10个数据帧S1至S10。现在我想用S1到S10以不同的组合创建另外10个数据帧T1到T10。每个T组将有9个S组。r在for循环中以不同组合绑定数据帧

实施例,

T1 = rbind(S2,S3,S4,S5,S6,S7,S8,S9,S10) # If T1 is the set, S1 should not be there.  
. 
. 
. 
T10 = rbind(S1,S2,S3,S4,S5,S6,S7,S8,S9) # T10 not there. 

我怎样才能产生T1到T10使用用于循环,假设S1至S10的数据帧被提供有相等数目的列和标头的通过了S1至S10相同。

任何帮助,高度赞赏。如果不是循环,建议任何其他功能。

谢谢。

回答

2

这里有一种方法

S1 <- data.frame(x=1) 
S2 <- data.frame(x=2) 
S3 <- data.frame(x=3) 

lst <- mget(grep("S\\d+", ls(), value = TRUE)) 
idx <- combn(1:length(lst), length(lst)-1)[, length(lst):1] 
res <- apply(idx, 2, function(x) { 
    do.call(rbind, lst[x]) 
}) 
names(res) <- paste0("T", 1:length(lst)) 
list2env(res, .GlobalEnv) 

apply使用引擎盖下的环路。