简体   繁体   English

如何在purrr :: map中使用dplyr :: filter

[英]How to use dplyr::filter inside purrr::map

I have a data.frame with several variables and I would like to get a list where each item is a variable of the data.frame filtered with a condition. 我有一个带有多个变量的data.frame,我想获得一个列表,其中每个项目都是使用条件过滤的data.frame的变量。

For exemple let's say I have something like that : 例如,假设我有类似的东西:

df <- tribble(
  ~ var1, ~ var2, ~ var3,
  4, 0, 0,
  2, 3, 1,
  0, 4, 0
  )
#    var1  var2  var3
#   <dbl> <dbl> <dbl>
# 1     4     0     0
# 2     2     3     1
# 3     0     4     0

And I want to get the list of the variable filtered >0 我想获得过滤的变量列表> 0

# $var1
# [1] 4 2
#
# $var2
# [1] 3 4
#
# $var3
# [1] 1

I tried several things but the closest I can get for now is something like 我尝试了几件事,但是现在我能得到的最接近的东西是

df %>% map(~filter(df, .>0))

and I would like to include a dplyr::select to get only the variable filtered. 并且我想包含dplyr::select以仅过滤变量。 But I can't figure out how to do that. 但是我不知道该怎么做。

Thanks for help and sorry for bad English, I hope it's still understandable. 感谢您的帮助,也很抱歉英语不好,我希望它仍然可以理解。

We can loop through the names . 我们可以遍历names Note that filter expects a data.frame/tbl_df . 请注意, filter需要data.frame/tbl_df With map , we are looping through the columns and it is a vector . 使用map ,我们遍历各列,它是一个vector So, in order to make filter work, map through the names , subset the column, apply the filter and unlist 因此,为了使filter的工作, map通过names ,子集列,应用filterunlist

map(names(df), ~ df %>% 
                  select(.x) %>%
                  filter(. >0) %>%
                  unlist(., use.names = FALSE))

Or with split 或与split

split.default(df, names(df)) %>%
            map(~  .x %>% 
                       filter(. > 0) %>%
                        pull(1))

NOTE: The OP's question is How to use dplyr::filter inside purrr::map 注意:OP的问题是How to use dplyr::filter inside purrr::map


Other ways without using dplyr::filter are 不使用dplyr::filter其他方法是

map(df, ~ keep(.x, .x != 0))

Or 要么

map(df, setdiff, 0)

Or 要么

map(df, ~ discard(.x, .x == 0))

Or using base R 或使用base R

lapply(df, setdiff, 0)
#$var1
#[1] 4 2

#$var2
#[1] 3 4

#$var3
#[1] 1

Using purrr::map we can do 使用purrr::map我们可以做

purrr::map(df, ~.[.!= 0])

#$var1
#[1] 4 2

#$var2
#[1] 3 4

#$var3
#[1] 1

Base R approach with lapply could be 带有lapply Base R方法可能是

lapply(df, function(x) x[x!= 0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM