Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

r - Vectorizing a function after group_by() in tidyverse

I was wondering if there is a way for my identify_outliers() call to become vectorized?

That is, instead of accepting only one variable like identify_outliers(Sepal.Length), it would handle multiple variables like identify_outliers(Sepal.Length:Petal.Width)?

Here is my reproducible code:

library(rstatix)
library(tidyverse)

iris %>%
  group_by(Species) %>%
  identify_outliers(Sepal.Length:Petal.Width)
question from:https://stackoverflow.com/questions/66056735/vectorizing-a-function-after-group-by-in-tidyverse

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use one of map functions :

library(rstatix)
library(tidyverse)

map_df(names(select(iris, Sepal.Length:Petal.Width)), ~{
  iris %>%
    group_by(Species) %>%
    identify_outliers(all_of(.x))
})

#   Species    Sepal.Length Sepal.Width Petal.Length Petal.Width is.outlier is.extreme
#   <fct>             <dbl>       <dbl>        <dbl>       <dbl> <lgl>      <lgl>     
# 1 virginica           4.9         2.5          4.5         1.7 TRUE       FALSE     
# 2 setosa              5.7         4.4          1.5         0.4 TRUE       FALSE     
# 3 setosa              4.5         2.3          1.3         0.3 TRUE       FALSE     
# 4 virginica           7.7         3.8          6.7         2.2 TRUE       FALSE     
# 5 virginica           6           2.2          5           1.5 TRUE       FALSE     
# 6 virginica           7.9         3.8          6.4         2   TRUE       FALSE     
# 7 setosa              4.3         3            1.1         0.1 TRUE       FALSE     
# 8 setosa              4.6         3.6          1           0.2 TRUE       FALSE     
# 9 setosa              4.8         3.4          1.9         0.2 TRUE       FALSE     
#10 setosa              5.1         3.8          1.9         0.4 TRUE       FALSE     
#11 versicolor          5.1         2.5          3           1.1 TRUE       FALSE     
#12 setosa              5.1         3.3          1.7         0.5 TRUE       FALSE     
#13 setosa              5           3.5          1.6         0.6 TRUE       TRUE    

Similarly, using imap_dfr :

imap_dfr(select(iris, Sepal.Length:Petal.Width), ~{
  iris %>%
    group_by(Species) %>%
    identify_outliers(all_of(.y))
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...