听起来像您可能希望将数据帧从宽转换为长格式,并将所有布尔列收集到一个列中。例如:使用
library(dplyr)
library(tidyr)
df %>%
gather(subset.variable, logic, -date, -value) %>%
filter(logic) %>%
ggplot(aes(date, value)) +
geom_point() + # using geom_point for illustration
facet_wrap(~subset.variable)
的样本数据:
set.seed(123)
n = 200
df <- data.frame(
date = seq.Date(from = as.Date("1999-01-01"),
to = as.Date("1999-01-01") + n - 1,
by = 1),
value = rpois(n, lambda = 2),
x1 = sample(c(TRUE, FALSE), n, replace = T),
x2 = sample(c(TRUE, FALSE), n, replace = T),
x3 = sample(c(TRUE, FALSE), n, replace = T),
x4 = sample(c(TRUE, FALSE), n, replace = T),
x5 = sample(c(TRUE, FALSE), n, replace = T),
x6 = sample(c(TRUE, FALSE), n, replace = T)
)
> head(df)
date value x1 x2 x3 x4 x5 x6
1 1999-01-01 1 TRUE FALSE TRUE TRUE TRUE FALSE
2 1999-01-02 3 FALSE TRUE FALSE TRUE FALSE FALSE
3 1999-01-03 2 FALSE FALSE TRUE TRUE TRUE TRUE
4 1999-01-04 4 FALSE FALSE TRUE TRUE FALSE FALSE
5 1999-01-05 4 TRUE TRUE TRUE TRUE FALSE TRUE
6 1999-01-06 0 FALSE TRUE FALSE FALSE TRUE FALSE
完美!奇迹般有效。谢谢 – user2969402