计算R中时间序列的日模式
我试图计算这个时间序列的每日模式。在下面的示例数据中,我希望看到windDir.c列每天的模式 由于没有colMode参数,因此不知道如何使用apply.daily包装器。因此,我尝试在period.apply中使用自定义函数,但没有效果。下面是我尝试的代码以及dput 可复制数据:计算R中时间序列的日模式,r,dataframe,time-series,xts,R,Dataframe,Time Series,Xts,我试图计算这个时间序列的每日模式。在下面的示例数据中,我希望看到windDir.c列每天的模式 由于没有colMode参数,因此不知道如何使用apply.daily包装器。因此,我尝试在period.apply中使用自定义函数,但没有效果。下面是我尝试的代码以及dput 可复制数据: wind.d <- structure(list(date = structure(c(1280635200, 1280635200, 1280635200, 1280635200, 1280635200,
wind.d <- structure(list(date = structure(c(1280635200, 1280635200, 1280635200,
1280635200, 1280635200, 1280635200, 1280635200, 1280721600, 1280721600,
1280721600, 1280721600, 1280721600, 1280721600, 1280721600, 1280808000,
1280808000, 1280808000, 1280808000, 1280808000, 1280808000), class = c("POSIXct",
"POSIXt"), tzone = ""), windDir.c = structure(c(4L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 6L, 5L, 5L, 4L, 5L, 5L
), .Label = c("15", "45", "75", "105", "135", "165", "195", "225",
"255", "285", "315", "345"), class = "factor")), .Names = c("date",
"windDir.c"), class = "data.frame", row.names = c(NA, -20L))
我们可以使用dplyr轻松做到这一点: 或基准R:
calMode <- function(x) {
ux <- unique(x)
return(ux[which.max(tabulate(match(x, ux)))])
}
myModes <- tapply(as.character(windDir.c), INDEX = date, FUN = calMode)
请注意,您尝试的代码和提供的dput输出不一致。dput输出不是xts对象,您提供的代码将仅适用于xts对象端点在您提供的data.frame上失败 假设wind.d确实是一个xts对象,您可以使用xts轻松实现这一点:
wind.d <- structure(c(105, 75, 75, 105, 105, 105, 105, 105, 105, 105, 105,
105, 135, 135, 165, 135, 135, 105, 135, 135), .Dim = c(20L, 1L),
index = structure(c(1280635200, 1280635200, 1280635200, 1280635200,
1280635200, 1280635200, 1280635200, 1280721600, 1280721600, 1280721600,
1280721600, 1280721600, 1280721600, 1280721600, 1280808000, 1280808000,
1280808000, 1280808000, 1280808000, 1280808000), tzone = "",
tclass = c("POSIXct", "POSIXt")), .indexCLASS = c("POSIXct", "POSIXt"),
tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "",
.Dimnames = list(NULL, "windDir.c"), class = c("xts", "zoo"))
apply.daily(x, function(x) which.max(tabulate(x)))
# windDir.c
# 2010-07-31 23:00:00 105
# 2010-08-01 23:00:00 105
# 2010-08-02 23:00:00 135
我们可以加载包modeest以使用函数mfv Most frequency Value
library(dplyr)
library(modeest)
wind.d %>% group_by(date) %>% summarise(mode = mfv(windDir.c))
输出:
date mode
1 2010-08-01 06:00:00 105
2 2010-08-02 06:00:00 105
3 2010-08-03 06:00:00 135
如果有多种模式,我们需要指定要检索的元素。否则它将返回一个错误。例如,第一个元素:
mfv(iris[iris$Species=="setosa", 1])
[1] 5.0 5.1
# dplyr
iris %>% group_by(Species) %>% summarise(mode = mfv(Sepal.Length)[1])
Species mode
1 setosa 5.0
2 versicolor 5.5
3 virginica 6.3
sqldf
对于那些对sqldf感兴趣的人,请使用:
迈克尔,谢谢你的意见。然而,当我运行您的代码时,我得到一个错误,错误是:error in unique.defaultx,nmax=nmax:unique仅适用于VectorHanks,以便使用我最初想要使用的函数找到一个简单的解决方案。为不一致性道歉-我刚刚学习如何处理xts时间序列对象。
date mode
1 2010-08-01 06:00:00 105
2 2010-08-02 06:00:00 105
3 2010-08-03 06:00:00 135
mfv(iris[iris$Species=="setosa", 1])
[1] 5.0 5.1
# dplyr
iris %>% group_by(Species) %>% summarise(mode = mfv(Sepal.Length)[1])
Species mode
1 setosa 5.0
2 versicolor 5.5
3 virginica 6.3
library(sqldf)
sqldf("SELECT date,
(SELECT [windDir.c]
FROM [wind.d]
WHERE date = tbl.date
GROUP BY [windDir.c]
ORDER BY count(*) DESC
LIMIT 1) AS mode
FROM (SELECT DISTINCT date
FROM [wind.d]) AS tbl")