【问题标题】:Keep getting error: Expecting a single value: [extent=2] in summarise不断收到错误:期望单个值:总结中的 [extent=2]
【发布时间】:2020-07-16 01:03:45
【问题描述】:

运行以下脚本时不断收到此错误 - 在 dplyr 包中使用汇总

analysis <- df %>% group_by(Year, Week, YearWeek, Cad.Name, CamName, Abs.ID, Place.Name, Pull.Name) %>% 
  summarise(visits=sum(Visits)) %>%
  group_by(Year, Week, YearWeek, Cad.Name, CamName, Abs.ID, Place.Name) %>% 
  summarise(video=unique(Video.Name),visits=sum(Visits)) %>%
  arrange(Year,Week)

Video.Name 是一个问题,需要在汇总之前将其添加到 groupby 以使其执行。我需要通过访问期间的唯一视频数来总结 - 有什么想法吗?我不断收到这些错误:

Error: Expecting a single value: [extent=2].
In addition: Warning messages:
1: Factor `YearWeek` contains implicit NA, consider using `forcats::fct_explicit_na` 
2: Factor `YearWeek` contains implicit NA, consider using `forcats::fct_explicit_na`

【问题讨论】:

  • 如果没有您的任何数据,很难准确地说。 See here 提出一个人们可以更轻松地帮助解决的 R 问题

标签: r error-handling dplyr


【解决方案1】:

错误应该来自unique(Video.Name),它的length 可以大于1,但summarise 每个组只返回一行。我们可以将其包装在list 中。

library(dplyr)
df %>%
   group_by(Year, Week, YearWeek, Cad.Name, CamName, 
        Abs.ID, Place.Name, Pull.Name) %>% 
   summarise(visits=sum(Visits)) %>%
   group_by(Year, Week, YearWeek, Cad.Name, CamName, Abs.ID, Place.Name) %>% 
   summarise(video=list(unique(Video.Name)),visits=sum(Visits)) %>%
   arrange(Year, Week)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 2017-01-26
    • 2020-08-27
    • 2017-10-29
    • 1970-01-01
    • 2019-07-18
    相关资源
    最近更新 更多