N08
N08

Reputation: 1315

R: Conditional combinations of dataframe

I want to create a dataframe from combinations of the var-column in following dataframe

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. 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?

Upvotes: 0

Views: 319

Answers (1)

guscht
guscht

Reputation: 893

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.

Upvotes: 1

Related Questions