从列创建布尔表达式

时间:2016-09-11 19:31:38

标签: r

我有一个这样的数据框:

var h1 = document.createElement("h1");                
var h1Text = document.createTextNode("Hello " + firstName + " " + lastName +" !");         
h1.appendChild(h1Text);                              
document.body.appendChild(h1); 

我想基于AND创建所有列的所有可能组合的布尔表达式,使用第一列作为基础。

基于上述df的输出示例:

set1,set2,set3
"test1","test12","test13"
"test2","test22","test23"

有没有简单的方法来制作它?我试过这个:

("test1" AND "test12" AND "test13")
("test1" AND "test22" AND "test23")
("test2" AND "test12" AND "test13")
("test2" AND "test22" AND "test23")

1 个答案:

答案 0 :(得分:0)

一个想法,首先我们创建一个新列来粘贴set2set3,以避免使用("test1" AND "test22" AND "test13")等字符串。然后,我们通过expand.gridpaste创建组合,即

df1$new <- do.call(paste, c(df1[,(2:3)], sep = ' AND '))
do.call(paste, c(expand.grid(df1[,-(2:3)]), sep = ' AND '))
#[1] "test1 AND test12 AND test13" "test2 AND test12 AND test13" "test1 AND test22 AND test23" "test2 AND test22 AND test23"

如果您想要所有组合,那么

do.call(paste, c(expand.grid(df1), sep = ' AND '))
#[1] "test1 AND test12 AND test13" "test2 AND test12 AND test13" "test1 AND test22 AND test13" "test2 AND test22 AND test13"
#[5] "test1 AND test12 AND test23" "test2 AND test12 AND test23" "test1 AND test22 AND test23" "test2 AND test22 AND test23"

数据

dput(df1)
structure(list(set1 = c("test1", "test2"), set2 = c("test12", 
"test22"), set3 = c("test13", "test23")), .Names = c("set1", 
"set2", "set3"), class = "data.frame", row.names = c(NA, -2L))

修改 由于您希望在每个"test"周围保留引号,然后

#as before,
v1 <- do.call(paste, c(expand.grid(df1), sep = ' AND '))
v2 <- paste0('(', sapply(lapply(strsplit(v1, ' AND '), function(i) dQuote(i)), 
                                         function(j) paste(j, collapse = ' AND ')), ')')

#1 (“test1” AND “test12” AND “test13”)
#2 (“test2” AND “test12” AND “test13”)
#3 (“test1” AND “test22” AND “test13”)
#4 (“test2” AND “test22” AND “test13”)
#5 (“test1” AND “test12” AND “test23”)
#6 (“test2” AND “test12” AND “test23”)
#7 (“test1” AND “test22” AND “test23”)
#8 (“test2” AND “test22” AND “test23”)