apply.yearly()适用于子集,但不适用于R中的完整时间序列数据集

apply.yearly()适用于子集,但不适用于R中的完整时间序列数据集,r,function,data-structures,subset,xts,R,Function,Data Structures,Subset,Xts,当我在数据集上运行以下代码时,会得到如下输出(显示部分输出): all_countries_ts[,grepl("Muslims", colnames(all_countries_ts))] Senegal Muslims Serbia Muslims Seychelles Muslims 1970-01-01 3693807 200000 170 2000-01-01 8936283

当我在数据集上运行以下代码时,会得到如下输出(显示部分输出):

all_countries_ts[,grepl("Muslims", colnames(all_countries_ts))]

             Senegal Muslims Serbia Muslims Seychelles Muslims
1970-01-01         3693807         200000                170
2000-01-01         8936283         529322                730
2010-01-01        11713126         527598                821
2015-01-01        13621382         471414                844
但是,当我尝试使用函数apply.yearly对其进行多年累加时,我只得到一个NA结果:

apply.yearly(all_countries_ts[,grepl("Muslims", colnames(all_countries_ts))], FUN = sum)

1970-01-01   NA
2000-01-01   NA
2010-01-01   NA
2015-01-01   NA
有趣的是,它可以处理一些输入,但不能处理其他输入。例如,如果我使用输入“不可知论者”而不是“穆斯林”,我会得到一个很好的结果。没有错误,所以我似乎无法弄清楚这里到底发生了什么

所有国家/地区存储为xts对象。需要注意的一点是,apply.yearly()始终处理此数据集的一个子集。我已经编写了一个函数,您可以在下面看到它:

sum_by_category <- function(religious_group, dataset) {
apply.yearly(dataset[,grepl(paste(religious_group), colnames(dataset))], FUN = 
sum)
}

country_search <- function(country_name, z){
  z <- foreach(i = 1:length(country_name), .combine = merge.xts) %do%{
    all_countries_ts[,grepl(country_name[i], colnames(all_countries_ts))]
  }
  return(z)}

我真的搞不清楚到底发生了什么,因为它使用的是一些输入,而不是其他输入。提前感谢您的帮助/见解

xts::apply.yearly
期望
x
参数可强制到
xts
对象。可能您的data.frame不是兼容的
xts
数据帧

apply.year的帮助说明:

参数

x     an time-series object coercible to xts
FUN   an R function
我基于
OP
共享的数据创建了一个示例数据,并将其转换为
xts
类<代码>应用。每年一次
在同一系统上正常工作

library(xts)

# Convert data.frame to xts class
all_countries_ts <- xts(df[,-1], order.by = df$Date)

#Now one can use `apply.yearly`
apply.yearly(all_countries_ts[,grepl("Muslims", colnames(all_countries_ts))], FUN = sum)

#                [,1]
# 1970-01-01  3893977
# 2000-01-01  9466335
# 2010-01-01 12241545
# 2015-01-01 14093640
数据:


df我们如何重现这个问题?通过复制粘贴
dput(…)
来共享数据是一种很好的方法start@Sunny我已经更新了我的答案。对于少数列,您的数据包含的
NA
。由于
NA
是传染性的,它的总总和变为
NA
。您必须将
sum
的附加参数设置为
na.rm=TRUE
library(xts)

# Convert data.frame to xts class
all_countries_ts <- xts(df[,-1], order.by = df$Date)

#Now one can use `apply.yearly`
apply.yearly(all_countries_ts[,grepl("Muslims", colnames(all_countries_ts))], FUN = sum)

#                [,1]
# 1970-01-01  3893977
# 2000-01-01  9466335
# 2010-01-01 12241545
# 2015-01-01 14093640
apply.yearly(all_countries_ts[,grepl("Muslims",colnames(all_countries_ts))],
                FUN = sum, na.rm = TRUE)

#                  [,1]
# 1970-01-01  570772699
# 2000-01-01 1292170756
# 2010-01-01 1571250533
# 2015-01-01 1734531709
df <- read.table(text = 
" Date             'Senegal Muslims' 'Serbia Muslims' 'Seychelles Muslims' Others
1970-01-01         3693807         200000                170               200
2000-01-01         8936283         529322                730              100
2010-01-01        11713126         527598                821              300
2015-01-01        13621382         471414                844              500",
header = TRUE, stringsAsFactors = FALSE)

#convert Date column to Date format
df$Date <- as.Date(df$Date)