在purrr :: map()中使用dplyr :: count()时出错 [英] Error using dplyr::count() within purrr::map()

查看:161
本文介绍了在purrr :: map()中使用dplyr :: count()时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中,我想将count()函数应用于数据集中的每个字符变量。

In this example I want to apply the count() function to every character variable in a dataset.

library(dplyr)
library(purrr)

nycflights13::flights %>% 
    select_if(is.character) %>% 
    map(., count)

但是我收到错误消息:

Error in UseMethod("groups") : no applicable method for 
'groups' applied to an object of class "character"

我不确定解释错误消息或更新我的代码。类似的代码适用于数字变量,但因子变量会产生与字符变量相似的错误消息

I'm not sure how to interpret the error message or update my code. Similar code works for numeric variables, but factor variables produce a similar error message to character variables

nycflights13::flights %>% 
    select_if(is.numeric) %>% 
    map(., mean, na.rm = TRUE)

nycflights13::flights %>% 
    select_if(is.character) %>% 
    mutate_all(as.factor) %>% 
    map(., count)


推荐答案

如果要获取带有值计数的小标题列表,可以使用

If you want a list of tibbles with value counts, you can use

nycflights13::flights %>% 
  select_if(is.character) %>% 
  map(~count(data.frame(x=.x), x))

这篇关于在purrr :: map()中使用dplyr :: count()时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆