列出所有因素组合
Listing ALL combinations of Factors
给定以下数据框:
data_frame = data.frame(food = c("pizza", "tacos", "nachos"), drinks = c("water", "coffee", "pop"))
data_frame
food drinks
1 pizza water
2 tacos coffee
3 nachos pop
有没有办法列出这些因素的所有组合?
例如:
food_combinations = c("none", pizza", "tacos", "nachos", "pizza & tacos", "pizza & nachos", "tacos & nachos", "pizza & tacos & nachos")
drink_combinations = c("none", coffee", "pop", "water", "coffee & pop", "coffee & water", "pop & water", "coffee & pop & water")
谢谢!
我们可以使用 combn
来执行 - 使用 lapply
遍历列,然后对元素序列进行嵌套循环,应用 combn
和 paste
lst1 <- lapply(data_frame, \(x) c("none", unlist(lapply(seq_len(length(x)),
\(i) combn(x, i, FUN = paste, collapse = " & ")))))
-输出
> lst1
$food
[1] "none" "pizza" "tacos" "nachos" "pizza & tacos" "pizza & nachos"
[7] "tacos & nachos" "pizza & tacos & nachos"
$drinks
[1] "none" "water" "coffee" "pop" "water & coffee" "water & pop" "coffee & pop"
[8] "water & coffee & pop"
-编辑:采纳了@akrun 的回复,因为它更简洁并且不依赖任何包
这里有一个替代方法,以函数的形式。参数 m
允许您选择每个短语中的最大元素。
df <- data.frame(
food = c("pizza", "tacos", "nachos"),
drinks = c("water", "coffee", "pop")
)
comb1 <- function(vector, m = length(vector)) {
if (m >= length(vector)) {
data <- unlist(lapply(seq_len(length(df$food)),
\(i) combn(df$food, i, paste, collapse = ' & ')))
}
else {
data <- unlist(lapply(seq_len(m),
\(i) combn(df$food, i, paste, collapse = ' & ')))
}
return(data)
}
渲染
> comb1(df$food)
[1] "none" "nachos" "pizza"
[4] "tacos" "nachos & pizza" "nachos & tacos"
[7] "pizza & tacos" "nachos & pizza & tacos"
> comb1(df$food,2)
[1] "none" "nachos" "pizza" "tacos" "nachos & pizza"
[6] "nachos & tacos" "pizza & tacos"
对于数据框上的列表,只需 lapply
> lapply(df, comb1)
$food
[1] "none" "nachos" "pizza"
[4] "tacos" "nachos & pizza" "nachos & tacos"
[7] "pizza & tacos" "nachos & pizza & tacos"
$drinks
[1] "none" "coffee" "pop"
[4] "water" "coffee & pop" "coffee & water"
[7] "pop & water" "coffee & pop & water"
> lapply(df, \(x) comb1(x,2))
$food
[1] "none" "nachos" "pizza" "tacos" "nachos & pizza"
[6] "nachos & tacos" "pizza & tacos"
$drinks
[1] "none" "coffee" "pop" "water" "coffee & pop"
[6] "coffee & water" "pop & water"
给定以下数据框:
data_frame = data.frame(food = c("pizza", "tacos", "nachos"), drinks = c("water", "coffee", "pop"))
data_frame
food drinks
1 pizza water
2 tacos coffee
3 nachos pop
有没有办法列出这些因素的所有组合?
例如:
food_combinations = c("none", pizza", "tacos", "nachos", "pizza & tacos", "pizza & nachos", "tacos & nachos", "pizza & tacos & nachos")
drink_combinations = c("none", coffee", "pop", "water", "coffee & pop", "coffee & water", "pop & water", "coffee & pop & water")
谢谢!
我们可以使用 combn
来执行 - 使用 lapply
遍历列,然后对元素序列进行嵌套循环,应用 combn
和 paste
lst1 <- lapply(data_frame, \(x) c("none", unlist(lapply(seq_len(length(x)),
\(i) combn(x, i, FUN = paste, collapse = " & ")))))
-输出
> lst1
$food
[1] "none" "pizza" "tacos" "nachos" "pizza & tacos" "pizza & nachos"
[7] "tacos & nachos" "pizza & tacos & nachos"
$drinks
[1] "none" "water" "coffee" "pop" "water & coffee" "water & pop" "coffee & pop"
[8] "water & coffee & pop"
-编辑:采纳了@akrun 的回复,因为它更简洁并且不依赖任何包
这里有一个替代方法,以函数的形式。参数 m
允许您选择每个短语中的最大元素。
df <- data.frame(
food = c("pizza", "tacos", "nachos"),
drinks = c("water", "coffee", "pop")
)
comb1 <- function(vector, m = length(vector)) {
if (m >= length(vector)) {
data <- unlist(lapply(seq_len(length(df$food)),
\(i) combn(df$food, i, paste, collapse = ' & ')))
}
else {
data <- unlist(lapply(seq_len(m),
\(i) combn(df$food, i, paste, collapse = ' & ')))
}
return(data)
}
渲染
> comb1(df$food)
[1] "none" "nachos" "pizza"
[4] "tacos" "nachos & pizza" "nachos & tacos"
[7] "pizza & tacos" "nachos & pizza & tacos"
> comb1(df$food,2)
[1] "none" "nachos" "pizza" "tacos" "nachos & pizza"
[6] "nachos & tacos" "pizza & tacos"
对于数据框上的列表,只需 lapply
> lapply(df, comb1)
$food
[1] "none" "nachos" "pizza"
[4] "tacos" "nachos & pizza" "nachos & tacos"
[7] "pizza & tacos" "nachos & pizza & tacos"
$drinks
[1] "none" "coffee" "pop"
[4] "water" "coffee & pop" "coffee & water"
[7] "pop & water" "coffee & pop & water"
> lapply(df, \(x) comb1(x,2))
$food
[1] "none" "nachos" "pizza" "tacos" "nachos & pizza"
[6] "nachos & tacos" "pizza & tacos"
$drinks
[1] "none" "coffee" "pop" "water" "coffee & pop"
[6] "coffee & water" "pop & water"