Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.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 <- zoo(rnorm(1000),as.Date("1973-02-01") + c(1:1000) - 1) data您可以对zoo系列使用aggregate()方法。聚合函数的具体内容取决于时间索引使用的类(例如,yearmon或Date等)。使用yearmon的一些人工数据: set.seed(1) x <- rnorm(3

这就像我有一个动物园对象,我想做的是每年分别应用一个特定的数据操作/函数

有什么方便的方法来完成这项任务吗

示例数据:

data <- zoo(rnorm(1000),as.Date("1973-02-01") + c(1:1000) - 1)

data您可以对zoo系列使用
aggregate()
方法。聚合函数的具体内容取决于时间索引使用的类(例如,yearmon或Date等)。使用yearmon的一些人工数据:

set.seed(1)
x <- rnorm(36) + 1:36
z_yearmon <- zoo(x, as.yearmon(2000) + 1:36/12)
aggregate(z_yearmon, function(x) floor(as.numeric(x)), mean)
##      2000      2001      2002      2003 
##  6.257619 17.729362 29.258357 35.585005 

您可以对zoo系列使用
aggregate()
方法。聚合函数的具体内容取决于时间索引使用的类(例如,yearmon或Date等)。使用yearmon的一些人工数据:

set.seed(1)
x <- rnorm(36) + 1:36
z_yearmon <- zoo(x, as.yearmon(2000) + 1:36/12)
aggregate(z_yearmon, function(x) floor(as.numeric(x)), mean)
##      2000      2001      2002      2003 
##  6.257619 17.729362 29.258357 35.585005 

您可以使用
xts
库中的
apply.yearly(x,FUN,…)
函数。它也适用于动物园对象

apply.yearly(data, mean)
输出:

 1973-12-31  1974-12-31  1975-10-28 
-0.01142834  0.02000210  0.02259480 

您可以使用
xts
库中的
apply.yearly(x,FUN,…)
函数。它也适用于动物园对象

apply.yearly(data, mean)
输出:

 1973-12-31  1974-12-31  1975-10-28 
-0.01142834  0.02000210  0.02259480