我有这张表:
col1 <- c("1","2", "3", "4", "5")
col1 <- sample(col1, 1000, replace=TRUE, prob=c(0.2, 0.2, 0.2, 0.2, 0.2))
col2 <- c("6","7", "8")
col2 <- sample(col2, 1000, replace=TRUE, prob=c(0.2, 0.4, 0.4))
col3 <- c("9","10", "11", "12")
col3 <- sample(col3, 1000, replace=TRUE, prob=c(0.1, 0.1, 0.4, 0.4))
col4 <- rexp( 1000, 0.5)
col5 <- rexp( 1000, 0.5)
id <- 1:1000
table_1 = data.frame(id, col1, col2, col3, col4, col5)
还有这个清单:
f <- function(set) {
n <- length(set)
masks <- 2^(1:n-1)
lapply( 1:2^n-1, function(u) set[ bitwAnd(u, masks) != 0 ] )
}
sample_list = f(min(col1):max(col3))
我想根据“sample_list”中的条目从“table_1”中选择行。例如:
select = as.integer(runif(1, min = 1, max = 512))
>select
381
my_select = sample_list[select]
sample_list[381]
[[1]]
[1] 3 4 5 6 7 9
有什么办法可以“快速”选择“table_1”中的所有行,其中(table_1$col1、table_1$col2、table_1$col3)具有“my_select”中包含的值吗?
这相当于:
subset(table_1, col1 %in% c("3", "4", "5") & col2 %in% c("6", "7") & col3 %in% c("9"))
谢谢你!