简体   繁体   English

R时间序列每周平均值

[英]R time-series weekly average

I am new to R and I'm trying to do some time-series analysis for Dogecoin and Dogecoin google searches (google trends) respectively. 我是R的新手,我想分别对Dogecoin和Dogecoin谷歌搜索(谷歌趋势)进行时间序列分析。 However, if you load more than 90 days of data for google trends it returns the weekly average search volume. 但是,如果您为Google趋势加载了90天以上的数据,它将返回每周的平均搜索量。 My Dogecoin data, however, is daily. 但是,我的狗狗币数据是每天的。 My Dogecoin data, however, is daily. 但是,我的狗狗币数据是每天的。 在此处输入图片说明 I would like to get my Dogecoin data to be the average for each week (Mo-So). 我想将我的Dogecoin数据作为每周的平均值(Mo-So)。 I have been googling on how to do this for about an hour and couldn't figure it out. 大约一个小时以来,我一直在搜索如何执行此操作,无法解决。 I have tried SMA with the TTR library, But I don't just want to average 7 consecutive days. 我已经在TTR库中尝试了SMA,但是我不希望平均连续7天。 I would like to get the average from Monday to Sunday, just like what I have with my google trends data. 我想获取周一至周日的平均值,就像我对Google趋势数据的了解一样。 Could someone help me do that? 有人可以帮我吗? Should I convert it to a ts first? 我应该先将其转换为ts吗?

If I had a dput() example I would be able to test it, but I think this should work: 如果我有一个dput()示例,我将可以对其进行测试,但是我认为这应该可行:

library(dplyr)
library(lubridate)
library(tidyr)

doge <- doge %>%
mutate(year = year(timestamp), month = month(timestamp), week = week(timestamp)) %>%
unite_("date", c("year", "month", "week"), sep ="-") %>%
group_by(date) %>%
summarise(mean_price_usd = mean(price_usd, na.rm = TRUE))

Try with the lubridate package:: 尝试使用lubridate软件包::

library(package=lubridate)

# Set Weeks number. Doge already set as class `Date`
Week_ <- week(Doge)

# Aggregate over week number
aggregate(Price_usd~Week, FUN=mean, data=Week_, na.rm=TRUE)

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

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