Mam listę o nazwie lst, która zawiera trzy iteracje, aw każdej iteracji są prognozy 2 lata. Chcę dokonać podzbioru i wyodrębnić z moich danych gender =2 w taki sposób, aby w końcu mieć dwie listy. jedna lista z gender 1 i druga lista z gender2.

iteration1 <- list(year1 =data.frame(age=c(10,11,12,13), district=c(1,2,3,4),gender=c(1,2,2,1)
                                ,weight=c(12.2,11.3,11.2,10.1)),
              year2 =data.frame(age=c(10,11,12,13,10,10), district=c(1,2,3,4,2,1),gender=c(1,2,2,1,1,1),weight=c(12.2,11.3,11.2,10.1,12.2,13.1)))

iteration2 <- list(year1 =data.frame(age=c(10,11,12,13), district=c(1,2,3,4),gender=c(2,2,1,1)
                                ,weight=c(12.2,11.3,11.2,10.1)),
              year2 =data.frame(age=c(10,11,12,13,13,13,12), district=c(1,2,3,4,1,3,3),gender=c(2,2,1,1,2,2,2),weight=c(12.2,11.3,11.2,10.1,10.9,11.9,15.1)))

iteration3 <- list(year1 =data.frame(age=c(10,11,12,13), district=c(1,2,3,4),gender=c(2,2,1,1)
                                ,weight=c(12.2,11.3,11.2,10.1)),
              year2 =data.frame(age=c(10,11,12,13,10,10,11,12), district=c(1,2,3,4,4,3,2,2),gender=c(2,2,1,1,2,2,1,2),weight=c(12.2,11.3,11.2,10.1,13.5,12.8,13.9,14.9)))

lst <- list(iteration1 = iteration1, iteration2 = iteration2, iteration3= iteration3  )
r
3
mehmo 19 czerwiec 2021, 17:09

4 odpowiedzi

Najlepsza odpowiedź

Mam nadzieję, że to masz na myśli:

library(purrr)

map(1:2, function(a){
  lst %>%
    map_dfr(~ .x %>% 
              map_dfr(~ .x %>% 
                        filter(gender == a)))
}) %>%
  set_names(paste("gender", 1:length(.)))

$`gender 1`
   age district gender weight
1   10        1      1   12.2
2   13        4      1   10.1
3   10        1      1   12.2
4   13        4      1   10.1
5   10        2      1   12.2
6   10        1      1   13.1
7   12        3      1   11.2
8   13        4      1   10.1
9   12        3      1   11.2
10  13        4      1   10.1
11  12        3      1   11.2
12  13        4      1   10.1
13  12        3      1   11.2
14  13        4      1   10.1
15  11        2      1   13.9

$`gender 2`
   age district gender weight
1   11        2      2   11.3
2   12        3      2   11.2
3   11        2      2   11.3
4   12        3      2   11.2
5   10        1      2   12.2
6   11        2      2   11.3
7   10        1      2   12.2
8   11        2      2   11.3
9   13        1      2   10.9
10  13        3      2   11.9
11  12        3      2   15.1
12  10        1      2   12.2
13  11        2      2   11.3
14  10        1      2   12.2
15  11        2      2   11.3
16  10        4      2   13.5
17  10        3      2   12.8
18  12        2      2   14.9
4
Anoushiravan R 19 czerwiec 2021, 15:04

Możesz to zrobić

library(tidyverse)

map_dfr(lst, \(x) map_dfr(x, ~.x)) %>% split(.$gender)
#> $`1`
#>    age district gender weight
#> 1   10        1      1   12.2
#> 4   13        4      1   10.1
#> 5   10        1      1   12.2
#> 8   13        4      1   10.1
#> 9   10        2      1   12.2
#> 10  10        1      1   13.1
#> 13  12        3      1   11.2
#> 14  13        4      1   10.1
#> 17  12        3      1   11.2
#> 18  13        4      1   10.1
#> 24  12        3      1   11.2
#> 25  13        4      1   10.1
#> 28  12        3      1   11.2
#> 29  13        4      1   10.1
#> 32  11        2      1   13.9
#> 
#> $`2`
#>    age district gender weight
#> 2   11        2      2   11.3
#> 3   12        3      2   11.2
#> 6   11        2      2   11.3
#> 7   12        3      2   11.2
#> 11  10        1      2   12.2
#> 12  11        2      2   11.3
#> 15  10        1      2   12.2
#> 16  11        2      2   11.3
#> 19  13        1      2   10.9
#> 20  13        3      2   11.9
#> 21  12        3      2   15.1
#> 22  10        1      2   12.2
#> 23  11        2      2   11.3
#> 26  10        1      2   12.2
#> 27  11        2      2   11.3
#> 30  10        4      2   13.5
#> 31  10        3      2   12.8
#> 33  12        2      2   14.9

Utworzony 19.06.2021 przez pakiet reprex (v2.0.0)

3
AnilGoyal 19 czerwiec 2021, 16:39

Opcja z bind_rows

library(dplyr)
library(purrr)
map_dfr(lst, bind_rows) %>% 
    group_split(gender)

-wynik

[[1]]
# A tibble: 15 x 4
     age district gender weight
   <dbl>    <dbl>  <dbl>  <dbl>
 1    10        1      1   12.2
 2    13        4      1   10.1
 3    10        1      1   12.2
 4    13        4      1   10.1
 5    10        2      1   12.2
 6    10        1      1   13.1
 7    12        3      1   11.2
 8    13        4      1   10.1
 9    12        3      1   11.2
10    13        4      1   10.1
11    12        3      1   11.2
12    13        4      1   10.1
13    12        3      1   11.2
14    13        4      1   10.1
15    11        2      1   13.9

[[2]]
# A tibble: 18 x 4
     age district gender weight
   <dbl>    <dbl>  <dbl>  <dbl>
 1    11        2      2   11.3
 2    12        3      2   11.2
 3    11        2      2   11.3
 4    12        3      2   11.2
 5    10        1      2   12.2
 6    11        2      2   11.3
 7    10        1      2   12.2
 8    11        2      2   11.3
 9    13        1      2   10.9
10    13        3      2   11.9
11    12        3      2   15.1
12    10        1      2   12.2
13    11        2      2   11.3
14    10        1      2   12.2
15    11        2      2   11.3
16    10        4      2   13.5
17    10        3      2   12.8
18    12        2      2   14.9
3
akrun 19 czerwiec 2021, 17:44

Używanie purrr:

library(dplyr)
library(purrr)

lst1 <- map(lst, ~ map(., filter, gender == 1))
lst2 <- map(lst, ~ map(., filter, gender == 2))

lst1

$iteration1
$iteration1$year1
  age district gender weight
1  10        1      1   12.2
2  13        4      1   10.1

$iteration1$year2
  age district gender weight
1  10        1      1   12.2
2  13        4      1   10.1
3  10        2      1   12.2
4  10        1      1   13.1


$iteration2
$iteration2$year1
  age district gender weight
1  12        3      1   11.2
2  13        4      1   10.1

$iteration2$year2
  age district gender weight
1  12        3      1   11.2
2  13        4      1   10.1


$iteration3
$iteration3$year1
  age district gender weight
1  12        3      1   11.2
2  13        4      1   10.1

$iteration3$year2
  age district gender weight
1  12        3      1   11.2
2  13        4      1   10.1
3  11        2      1   13.9

lst2

$iteration1
$iteration1$year1
  age district gender weight
1  11        2      2   11.3
2  12        3      2   11.2

$iteration1$year2
  age district gender weight
1  11        2      2   11.3
2  12        3      2   11.2


$iteration2
$iteration2$year1
  age district gender weight
1  10        1      2   12.2
2  11        2      2   11.3

$iteration2$year2
  age district gender weight
1  10        1      2   12.2
2  11        2      2   11.3
3  13        1      2   10.9
4  13        3      2   11.9
5  12        3      2   15.1


$iteration3
$iteration3$year1
  age district gender weight
1  10        1      2   12.2
2  11        2      2   11.3

$iteration3$year2
  age district gender weight
1  10        1      2   12.2
2  11        2      2   11.3
3  10        4      2   13.5
4  10        3      2   12.8
5  12        2      2   14.9

Używając podstawy R, otrzymasz to samo wyjście z następującymi liniami:

lst1 <- lapply(lst, function(x) lapply(x, function(y) subset(y, gender == 1)))
lst2 <- lapply(lst, function(x) lapply(x, function(y) subset(y, gender == 2)))
1
Phil 19 czerwiec 2021, 14:52