从城市,条件和订单号的数据框开始,我尝试创建 n 可能的城市和订单号组合。条件([1],[2],[3],[1]& [2],[1]& [3],[2]& [3],[1]& [2]& ; [3])。
library(gtools)
set.seed(123)
dat <- data.frame(City = c(rep("St. Louis", 3), rep("Chicago", 2)),
Condition = c(rep("A", 3), rep("B", 2)),
Order.No = round(runif(5,10,100),0))
城市分裂&amp;条件:
dat_groups <- lapply(split(dat, list(dat$City, dat$Condition)), function(x) {
x$Order.No
})
> dat_groups
$Chicago.A
numeric(0)
$`St. Louis.A`
[1] 36 81 47
$Chicago.B
[1] 89 95
$`St. Louis.B`
numeric(0)
我可以使用while&(#34; combn&#34;作为 n 的容器接近组合解决方案,但是我无法以可接受的格式将输出保存到列表对象。
combn <- 4
counter <- 0
while (counter <= combn) {
counter <- counter + 1
temp <- lapply(dat_groups, function(x) {
n_obs <- length(x)
if(n_obs == 0) {
NA
}
if(n_obs > 0 & n_obs >= counter) {
combinations(n_obs, counter, x)
} else {
NA
}
})
print(temp)
}
$Chicago.A
[1] NA
$`St. Louis.A`
[,1]
[1,] 36
[2,] 47
[3,] 81
$Chicago.B
[,1]
[1,] 89
[2,] 95
$`St. Louis.B`
[1] NA
$Chicago.A
[1] NA
$`St. Louis.A`
[,1] [,2]
[1,] 36 47
[2,] 36 81
[3,] 47 81
$Chicago.B
[,1] [,2]
[1,] 89 95
$`St. Louis.B`
[1] NA
$Chicago.A
[1] NA
$`St. Louis.A`
[,1] [,2] [,3]
[1,] 36 47 81
...............
truncated
上面的代码通过列出所有单一组合来接近,然后是双打,然后是每个城市和附近的三元组。条件,但我无法弄清楚如何删除NA,关闭孔然后保存到下面的列表对象。
所需的最终解决方案应如下所示:
[[1]]
[1] "36"
[[2]]
[1] "81"
[[3]]
[1] "47"
[[4]]
[1] "36" "81"
[[5]]
[1] "36" "47"
[[6]]
[1] "81" "47"
[[7]]
[1] "36" "81" "47"
[[8]]
[1] "89"
[[9]]
[1] "95"
[[10]]
[1] "89" "95"
感谢您查看并提供任何帮助。
答案 0 :(得分:2)
您可以使用dplyr
获取列表的data.frame:
library(dplyr)
newdat <- dat %>% group_by(City, Condition) %>%
summarise(lists = list(lapply(1:n(),
function(z){combinations(v=Order.No, r=z, n=n())})))
newdat
Source: local data frame [2 x 3]
Groups: City [?]
City Condition lists
(fctr) (fctr) (chr)
1 Chicago B <list[2]>
2 St. Louis A <list[3]>
newdat$lists
列现在包含每个城市级别的所有子样本:条件列表中的条件。
为了使其格式与您想要的输出格式相同,我们需要进行一些列表争论:
unlist(lapply(unlist(newdat$lists, recursive = FALSE),
function(x){as.list(data.frame(t(x)))}), recursive = FALSE)
$X1
[1] 89
$X2
[1] 95
$t.x.
[1] 89 95
$X1
[1] 36
$X2
[1] 47
$X3
[1] 81
$X1
[1] 36 47
$X2
[1] 36 81
$X3
[1] 47 81
$t.x.
[1] 36 47 81
编辑:作为一个功能:
lister <- function(data, numgroups){
data %>% group_by(City, Condition) %>%
summarise(lists = list(lapply(1:min(numgroups, n()),
function(z){combinations(v=Order.No, r=z, n=n())})))
}
例如:
lister(dat, 2)