dplyr:使用select_if()进行条件列选择 [英] dplyr: conditional column selection using select_if()
本文介绍了dplyr:使用select_if()进行条件列选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
上一个问题的后续操作...
A follow-up from a previous question...
如何根据类型选择所有列,除了基于选择助手的一列功能?
How to select all columns based on a type, with the exception of one column based on a select helper function?
select_if(iris, is.numeric, vars(-contains("Width")))
Error: No tidyselect variables were registered
我将其存储在嵌套数据框中,并在purrr上运行它: map()会使工作流程选项稍微复杂化:
I have it within a nested dataframe, and am running it over a purrr::map() which slightly complicates the workflow options:
iris %>%
group_by(Species) %>%
nest %>%
mutate(data = map(data, ~ .x %>% select_if(is.numeric) %>% mutate(count = sum(rowSums(.))))) %>%
mutate(data = map(data, ~ .x %>% select_if(is.numeric) %>%
mutate_all(funs((. / count) * 100 )))) %>%
unnest
推荐答案
最简单,最清晰的方法是将2个 select
函数整合在一起:
The simplest and clearest way to do this is to pipe together 2 select
functions:
iris %>%
select_if(is.numeric) %>% # Select all numeric columns
select(-contains('Width')) %>% # Then drop 'Width' column(s)
head
Sepal.Length Petal.Length
1 5.1 1.4
2 4.9 1.4
3 4.7 1.3
4 4.6 1.5
5 5.0 1.4
6 5.4 1.7
即使在 map
函数:
iris %>%
group_by(Species) %>%
nest %>%
mutate(data = map(data, ~ .x %>%
select_if(is.numeric) %>%
select(-contains('Width')) %>%
mutate(count = sum(rowSums(.))))) %>%
mutate(data = map(data, ~ .x %>%
select_if(is.numeric) %>%
select(-contains('Width')) %>%
mutate_all(funs((. / count) * 100 )))) %>%
unnest
# A tibble: 150 x 4
Species Sepal.Length Petal.Length count
<fct> <dbl> <dbl> <dbl>
1 setosa 1.58 0.433 100
2 setosa 1.52 0.433 100
3 setosa 1.45 0.402 100
4 setosa 1.42 0.464 100
5 setosa 1.55 0.433 100
6 setosa 1.67 0.526 100
7 setosa 1.42 0.433 100
8 setosa 1.55 0.464 100
9 setosa 1.36 0.433 100
10 setosa 1.52 0.464 100
# ... with 140 more rows
这篇关于dplyr:使用select_if()进行条件列选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文