r - 如何在R中按月/年对变量求和?
问题描述
library(dplyr)
library(plotly)
library(lubridate)
googlesearch <- read.csv("multiTimeline.csv", header = FALSE, stringsAsFactors = FALSE)
googlesearch2 <- googlesearch [-1, ]
googlesearch2 <- googlesearch2 [-1, ]
colnames(googlesearch2)[1] <- 'Date'
colnames(googlesearch2)[2] <- 'NumberofSearch'
googlesearch2$Date <- as.Date(googlesearch2$Date)
googlesearch2 <- googlesearch2 %>%
filter(Date > "2015-01-04" & Date < "2018-05-27")
googlesearch3 <- googlesearch2 %>%
transform(googlesearch2$Date, Date = as.Date(as.character(Date), "%Y-%m-%d"))
googlesearch3 <- googlesearch2 %>%
mutate(month = format(Date, "%m"), year = format(Date, "%Y")) %>%
group_by(Date, yearMon = as.yearmon(Date, "%m-%d-%Y"))
googlesearch3$Date <- as.numeric(googlesearch3$NumberofSearch)
googlesearch3 <- googlesearch3 %>%
mutate(month = format(Date, "%m"), year = format(Date, "%Y")) %>%
group_by(Date, yearMon = as.yearmon(Date, "%m-%d-%Y")) %>%
summarise(NumberofSearch_sum = sum(NumberofSearch))
data <- tbl_df(googlesearch3)
data %>%
group_by(yearMon) %>%
summarise(NumberofSearch_mon = sum(NumberofSearch))
我知道这很混乱。我收到此错误,我不知道为什么。添加示例代码。
Error in summarise_impl(.data, dots) :
Evaluation error: invalid 'type' (character) of argument.
解决方案
由于缺乏可重现的示例,请尝试将示例代码的最后一个代码块替换为:
library(hablar)
data %>%
retype() %>%
group_by(yearMon) %>%
summarise(NumberofSearch_mon = sum(NumberofSearch))
也许它有效:)
推荐阅读
- android - 在 Unity 2019.1f31 中构建 Android 项目时出错
- c++ - STL c ++中不同类型的列表声明的错误
- python - 如何创建正则表达式模式以将字符从各种结构的字符串列表中提取出来?
- r - 干净地粘贴包含 NA 的列
- makecode - 如何制作 MicroBit MakeCode 批量代码创建器?
- amazon-web-services - 查询 ec2 实例中使用的 SSH 入站源 0.0.0.0/0
- reactjs - 如何将动画导出为对 mp4 做出本机反应
- javascript - 在构建 Atom 包时,如何在弹出菜单中显示列表?
- excel - VBA Excel - 根据复杂的规则将每日交货清单分类
- java - 如何在 Hibernate 中处理在数据库中共享相同复合 id 的多个表实体?