Home > Net >  how to use $ mark to get a sub-element of a list in dplyr pipe
how to use $ mark to get a sub-element of a list in dplyr pipe

Time:12-30

What if I want to use "$" to get the sub-elements of a list in dplyr pipe?

Problem from: I called boxplot.stats() on every circumstances (4 factors: value, metallic, bumpiness, reference):

match.raw %>% 
  select(-id) %>% 
  group_by(value, metallic, bumpiness, reference) %>% 
  group_split() %>% 
  map(select, diff) %>% 
  map(as.matrix) %>% 
  map(boxplot.stats)

Then I get a bunch of results of multiple boxplot.stats calls. I want to get the "out" information from every boxplot.stats result just like:

boxplot.stats(x)$out

Here is my data:

> head(match.raw, 20)
   id value metallic bumpiness reference match  diff
1   1   1.0      1.0       0.5       0.7  0.74  0.04
2   1   1.0      1.0       0.5       0.9  0.88 -0.02
3   1   1.0      0.0       0.5       0.3  0.30  0.00
4   1   0.0      0.5       0.5       0.3  0.32  0.02
5   1   1.0      0.0       0.5       0.7  0.46 -0.24
6   1   0.0      1.0       0.5       0.3  0.28 -0.02
7   1   0.0      1.0       0.5       0.7  0.72  0.02
8   1   1.0      0.5       0.5       0.5  0.56  0.06
9   1   0.5      0.0       0.5       0.9  0.84 -0.06
10  1   0.0      0.5       0.5       0.5  0.54  0.04
11  1   0.5      1.0       0.5       0.1  0.10  0.00
12  1   0.5      0.5       0.5       0.9  0.96  0.06
13  1   1.0      0.0       0.5       0.1  0.00 -0.10
14  1   0.0      0.5       0.5       0.9  0.92  0.02
15  1   1.0      0.5       0.5       0.9  0.94  0.04
16  1   1.0      0.5       0.5       0.1  0.28  0.18
17  1   1.0      1.0       0.5       0.1  0.10  0.00
18  1   0.0      0.5       0.5       0.1  0.22  0.12
19  1   1.0      0.0       0.5       0.5  0.00 -0.50
20  1   0.5      0.5       0.5       0.7  0.78  0.08

CodePudding user response:

Try this.

library(purrr); library(dplyr)

match.raw %>% 
  select(-id) %>% 
  group_by(value, metallic, bumpiness, reference) %>% 
  group_split() %>% 
  map(select, diff) %>% 
  map(as.matrix) %>% 
  map(boxplot.stats) %>%
  map(\(.) .$out) %>%
  unlist()

Your example data is everywhere numeric(0) in "out" though.

  • Related