R 构建具有所有变体的唯一组合的数据帧

R 构建具有所有变体的唯一组合的数据帧,r,R,我正在构建一些示例数据,需要能够获得3个变量因子的每个组合的一些数据 我有3个向量,每个向量有3个排列: fruit <- c("pears", "apples", "grapes") veg <- c("carrots", "cabbages", "broccoli") pets <- c("cats", "dogs", "fish") 水果我想我得到了27种组合。我忽略了会话 d = t(combn(c(fruit,pets,veg),3)) x = rep(0, nro

我正在构建一些示例数据,需要能够获得3个变量因子的每个组合的一些数据

我有3个向量,每个向量有3个排列:

fruit <- c("pears", "apples", "grapes")
veg <- c("carrots", "cabbages", "broccoli")
pets <- c("cats", "dogs", "fish")

水果我想我得到了27种组合。我忽略了
会话

d = t(combn(c(fruit,pets,veg),3))
x = rep(0, nrow(d))
for (i in 1:nrow(d)){
    if ( any(d[i,] %in% fruit) & any(d[i,] %in% pets) & any(d[i,] %in% veg) ){
    x[i] = 1
    }
}
d = d[x == 1,]

n = nrow(d) * length(Date_Range)
DATE = rep(Date_Range,nrow(d))

D = d[rep(seq_len(nrow(d)), each=NROW(Date_Range)),]
OUTPUT = cbind(D,DATE)
head(OUTPUT)
#                              DATE   
#[1,] "pears" "cats" "carrots" "17167"
#[2,] "pears" "cats" "carrots" "17168"
#[3,] "pears" "cats" "carrots" "17169"
#[4,] "pears" "cats" "carrots" "17170"
#[5,] "pears" "cats" "carrots" "17171"
#[6,] "pears" "cats" "carrots" "17172"

我想我得到了27个组合。我忽略了
会话

d = t(combn(c(fruit,pets,veg),3))
x = rep(0, nrow(d))
for (i in 1:nrow(d)){
    if ( any(d[i,] %in% fruit) & any(d[i,] %in% pets) & any(d[i,] %in% veg) ){
    x[i] = 1
    }
}
d = d[x == 1,]

n = nrow(d) * length(Date_Range)
DATE = rep(Date_Range,nrow(d))

D = d[rep(seq_len(nrow(d)), each=NROW(Date_Range)),]
OUTPUT = cbind(D,DATE)
head(OUTPUT)
#                              DATE   
#[1,] "pears" "cats" "carrots" "17167"
#[2,] "pears" "cats" "carrots" "17168"
#[3,] "pears" "cats" "carrots" "17169"
#[4,] "pears" "cats" "carrots" "17170"
#[5,] "pears" "cats" "carrots" "17171"
#[6,] "pears" "cats" "carrots" "17172"

您能提供一个预期的输出示例吗?
会话
向量的意义是什么?你一点也不提。@Gregor只是一个指标,用来显示每个因子变量。实际用例是网站数据,实际向量是登录页、设备类别(手机、桌面、平板电脑)和频道(谷歌、Facebook等)。请参阅expand.grid函数:
expand.grid(日期范围、水果、蔬菜、宠物)
@Dave2e您也需要
Date\u Range
。您能提供一个预期的输出示例吗?
会话
向量的意义是什么?你一点也不提。@Gregor只是一个指标,用来显示每个因子变量。实际用例是网站数据,实际向量是登录页、设备类别(手机、桌面、平板电脑)和频道(谷歌、Facebook等)。请参阅expand.grid函数:
expand.grid(日期范围、水果、蔬菜、宠物)
@Dave2e您也需要
Date\u Range
在那里。OP也需要
Date\u Range
在那里。您好,感谢您抽出时间回答。我不知道这里的礼节如何。为了简单起见,我更喜欢使用expand.grid在注释中给出的答案,但我不能接受注释@d、 b哦,好的。还是谢谢你!OP希望在那里也有
date\u range
。您好,感谢您抽出时间回答。我不知道这里的礼节如何。为了简单起见,我更喜欢使用expand.grid在注释中给出的答案,但我不能接受注释@d、 b哦,好的。还是谢谢你!