我正在使用R编程语言。
我有以下数据集:
factor <- c(1,2,3,4,5,6,7,8,9,10)
var_1 <- as.factor(sample(factor, 10000, replace=TRUE, prob=c(0.1,0.1,0.1,0.1,0.1, 0.1,0.1,0.1,0.1,0.1)))
var_2 <- as.factor(sample(factor, 10000, replace=TRUE, prob=c(0.1,0.1,0.1,0.1,0.1, 0.1,0.1,0.1,0.1,0.1)))
var_3 <- as.factor(sample(factor, 10000, replace=TRUE, prob=c(0.1,0.1,0.1,0.1,0.1, 0.1,0.1,0.1,0.1,0.1)))
var_4 <- as.factor(sample(factor, 10000, replace=TRUE, prob=c(0.1,0.1,0.1,0.1,0.1, 0.1,0.1,0.1,0.1,0.1)))
var_5 <- as.factor(sample(factor, 10000, replace=TRUE, prob=c(0.1,0.1,0.1,0.1,0.1, 0.1,0.1,0.1,0.1,0.1)))
my_data = data.frame(var_1, var_2, var_3, var_4, var_5)
我还有另一个“条件”数据集,将用于查询此数据框:
conditions = data.frame(cond_1 = c("1,3,4", "4,5,6"), cond_2 = c("5,6", "7,8,9"))
我的问题是:
我尝试运行以下命令,根据第一行“条件”从“my_data”中选择行,但这会返回一个空结果:
my_data[my_data$var_1 %in% unlist(conditions[1,1]) &
my_data$var_2 %in% unlist(conditions[1,2]), ]
[1] var_1 var_2 var_3 var_4 var_5
<0 rows> (or 0-length row.names)
我试图通过“检查”这些条件来进一步了解这一点:
class(conditions[1,1])
[1] "character"
这让我觉得“unlist()”命令不起作用,因为条件本身是“字符”而不是“列表”。
这里是否可以使用与“unlist()”命令相同的等效命令来运行上述语句?
总的来说,我试图产生与我从这段代码中得到的结果相同的结果,但保持了我在上面使用的格式:
my_data[my_data$var_1 %in% c("1", "3", "4") &
my_data$var_2 %in% c("5", "6"), ]
谢谢
参考:
Selecting Rows of Data Based on Multiple Conditions