Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中按周、月等汇总数据_R - Fatal编程技术网

在R中按周、月等汇总数据

在R中按周、月等汇总数据,r,R,我需要汇总我的数据 Data Kg 1 2013-03-01 271 2 2013-03-06 374 3 2013-03-07 51 4 2013-03-12 210 5 2013-03-13 698 6 2013-03-15 328 每周或每月。我找到了这个答案,但我真的不明白答案。谁能告诉我怎么做这个案子。Thanx上述答案建议您使用xts软件包 library(xts) ## create you zoo ob

我需要汇总我的数据

            Data  Kg
    1 2013-03-01 271
    2 2013-03-06 374
    3 2013-03-07  51
    4 2013-03-12 210
    5 2013-03-13 698
    6 2013-03-15 328

每周或每月。我找到了这个答案,但我真的不明白答案。谁能告诉我怎么做这个案子。Thanx

上述答案建议您使用
xts
软件包

library(xts)
## create you zoo objects using your data
## you replace text argument by read.zoo(yourfile, header = TRUE)
x.zoo <- read.zoo(text='  Data  Kg         
+     1 2013-03-01 271
+     2 2013-03-06 374
+     3 2013-03-07  51
+     4 2013-03-12 210
+     5 2013-03-13 698
+     6 2013-03-15 328',header=TRUE)
### then aggregate
apply.weekly(x.zoo, mean)   ## per week
apply.monthly(x.zoo, mean)   ## per month
库(xts)
##使用数据创建动物园对象
##将文本参数替换为read.zoo(yourfile,header=TRUE)

x、 zoo或者,您可以使用
tapply
按周分组申请。在这里,我使用
lubridate
包从日期中提取周部分

# fake data
df <- structure(list(Datechar = c("2013-03-01", "2013-03-06", "2013-03-07", 
    "2013-03-12", "2013-03-13", "2013-03-15"), Kg = c(271L, 374L, 
    51L, 210L, 698L, 328L)), .Names = c("Datechar", "Kg"), class = "data.frame", row.names = c("1", 
    "2", "3", "4", "5", "6"))
# convert character to date
df$Date <- as.Date(df$Datechar)

# calculate mean kg for each week
library(lubridate)
tapply(df$Kg, week(df$Date), mean)
tapply(df$Kg, month(df$Date), mean)
#伪造数据
df