简体   繁体   English

R:数据框的条件组合

[英]R: Conditional combinations of dataframe

I want to create a dataframe from combinations of the var -column in following dataframe 我想从以下数据框中的var -column组合创建一个数据框

data <- data.frame("var"=c("x", "y", "z", "xy", "xz"),
                   "val"=c("1", "2", "3", "4",  "5"))

Unlike expand.grid I have the requirement that the combinations in var cannot contain each letter more than once. expand.grid不同,我要求var中的组合不能包含每个字母多次。 So the resulting dataframe must become 因此,结果数据框必须变为

dataRes <- data.frame("var"=c("x+y+z", "y+xz", "xy+z"),
                      "val"=c("6",     "7",    "7"))

Here is a second example 这是第二个例子

data <- data.frame("var"=c("x", "y", "z", "xy", "xz", "yz"),
                   "val"=c("1", "2", "3", "4",  "5", "6"))

where the desired output is 所需的输出是

dataRes <- data.frame("var"=c("x+y+z", "y+xz", "xy+z", "x+yz"),
                      "val"=c("6",     "7",    "7", "7"))

Is there a generic function in R for this, or do I simply have to make all combinations and then do a string-search to weed out all combinations where a letter appears more than once? R中是否有为此通用的功能,还是我只需要简单地进行所有组合,然后进行字符串搜索以淘汰字母出现多次的所有组合?

This follows your suggestion of making all combinations and then weeding out the ones where one of the variables exists more than once: 这是您提出的所有组合建议,然后剔除其中一个变量存在多个的变量:

x <- 3;y <- 2;z <- 4;vars <- c("x", "y", "z");oper <- c("+", "*")
combinations <- expand.grid(vars, oper, vars, oper,  vars)
combinations <- combinations[apply(combinations[c(1,3,5)], 1, FUN = anyDuplicated)==0, ]
pairs <- do.call(paste, c(combinations, sep=""))
result <- data.frame(expr = pairs, result = sapply(pairs, function(k) eval(parse(text = k))), row.names = 1:length(pairs))
result

I am relatively sure that there is no dedicated command for this. 我相对确定没有专用的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM