一种可能的解决方案,使用purrr::map()
和一些数据争吵到长/宽。可能不是最有效或最优雅的解决方案,但它确实发挥了作用。
library(tidyverse)
# sample data
frame <- data.frame(
id = letters[seq( from = 1, to = 10 )], a = rnorm(10, 4), b = rnorm(10, 6), c=rnorm(10, 5),
d = rnorm(10, 2),e=rnorm(10, 5), f = rnorm(10, 2))
# list of possible combinations
list_of_combinations <- list(
c(1),
c(2),
c(3),
c(1,2),
c(1,3),
c(2,3),
c(1,2,3)
)
# data in long format and a category variable (for each "chunk")
df_long <- frame %>% pivot_longer(-id) %>%
mutate(
cat = case_when(
(name %in% c("a", "b")) ~ 1L,
(name %in% c("c", "d")) ~ 2L,
(name %in% c("e", "f")) ~ 3L)
)
df_long
#> # A tibble: 60 x 4
#> id name value cat
#> <chr> <chr> <dbl> <int>
#> 1 a a 3.93 1
#> 2 a b 4.66 1
#> 3 a c 2.78 2
#> 4 a d 2.35 2
#> 5 a e 5.93 3
#> 6 a f -0.500 3
#> 7 b a 5.11 1
#> 8 b b 5.37 1
#> 9 b c 4.61 2
#> 10 b d 3.58 2
#> # … with 50 more rows
# map list to generate a list of each combination and then map it back into wide format
final_list_of_dfs <- list_of_combinations %>% map( ~ df_long %>% filter(cat %in% .x)) %>%
map(~ .x %>% select(-cat) %>% pivot_wider(names_from = "name", values_from = "value"))
glimpse(final_list_of_dfs)
#> List of 7
#> $ : tibble [10 × 3] (S3: tbl_df/tbl/data.frame)
#> ..$ id: chr [1:10] "a" "b" "c" "d" ...
#> ..$ a : num [1:10] 3.93 5.11 4.16 2.59 2.69 ...
#> ..$ b : num [1:10] 4.66 5.37 3.26 5.52 6.29 ...
#> $ : tibble [10 × 3] (S3: tbl_df/tbl/data.frame)
#> ..$ id: chr [1:10] "a" "b" "c" "d" ...
#> ..$ c : num [1:10] 2.78 4.61 3.8 3.06 4.68 ...
#> ..$ d : num [1:10] 2.353 3.579 0.744 1.582 3.377 ...
#> $ : tibble [10 × 3] (S3: tbl_df/tbl/data.frame)
#> ..$ id: chr [1:10] "a" "b" "c" "d" ...
#> ..$ e : num [1:10] 5.93 3.89 5.43 3.88 5.51 ...
#> ..$ f : num [1:10] -0.5 0.941 3.703 2.035 0.611 ...
#> $ : tibble [10 × 5] (S3: tbl_df/tbl/data.frame)
#> ..$ id: chr [1:10] "a" "b" "c" "d" ...
#> ..$ a : num [1:10] 3.93 5.11 4.16 2.59 2.69 ...
#> ..$ b : num [1:10] 4.66 5.37 3.26 5.52 6.29 ...
#> ..$ c : num [1:10] 2.78 4.61 3.8 3.06 4.68 ...
#> ..$ d : num [1:10] 2.353 3.579 0.744 1.582 3.377 ...
#> $ : tibble [10 × 5] (S3: tbl_df/tbl/data.frame)
#> ..$ id: chr [1:10] "a" "b" "c" "d" ...
#> ..$ a : num [1:10] 3.93 5.11 4.16 2.59 2.69 ...
#> ..$ b : num [1:10] 4.66 5.37 3.26 5.52 6.29 ...
#> ..$ e : num [1:10] 5.93 3.89 5.43 3.88 5.51 ...
#> ..$ f : num [1:10] -0.5 0.941 3.703 2.035 0.611 ...
#> $ : tibble [10 × 5] (S3: tbl_df/tbl/data.frame)
#> ..$ id: chr [1:10] "a" "b" "c" "d" ...
#> ..$ c : num [1:10] 2.78 4.61 3.8 3.06 4.68 ...
#> ..$ d : num [1:10] 2.353 3.579 0.744 1.582 3.377 ...
#> ..$ e : num [1:10] 5.93 3.89 5.43 3.88 5.51 ...
#> ..$ f : num [1:10] -0.5 0.941 3.703 2.035 0.611 ...
#> $ : tibble [10 × 7] (S3: tbl_df/tbl/data.frame)
#> ..$ id: chr [1:10] "a" "b" "c" "d" ...
#> ..$ a : num [1:10] 3.93 5.11 4.16 2.59 2.69 ...
#> ..$ b : num [1:10] 4.66 5.37 3.26 5.52 6.29 ...
#> ..$ c : num [1:10] 2.78 4.61 3.8 3.06 4.68 ...
#> ..$ d : num [1:10] 2.353 3.579 0.744 1.582 3.377 ...
#> ..$ e : num [1:10] 5.93 3.89 5.43 3.88 5.51 ...
#> ..$ f : num [1:10] -0.5 0.941 3.703 2.035 0.611 ...
由reprex 包于 2021-03-29 创建(v1.0.0)