首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >时间序列运算

时间序列运算
EN

Stack Overflow用户
提问于 2015-12-01 18:39:53
回答 1查看 147关注 0票数 0

我有一个从1902年到2014年的时间序列A (ts_A),frequency=12,每个月的值和某一年(以及随后的月份)的NA值。

代码语言:javascript
复制
ts_A 

 year gen feb mar ... dec  
 1902 300 525 652     524
 1903 NA  NA  NA  ...  NA
 .... ... ... ... ... ...
 2014 742 135 699 586 458

然而,我有另一个从1902年到2014年的时间序列(ts_B),frequency=1 (年分布)

代码语言:javascript
复制
year value
1902  6524
1903  5682
....  ....
2014  5984

只有在ts_B中存在该年的值时,我才会使用ts_A中的数据NA,因为稍后我会用一个比例来替换它们。

EN

回答 1

Stack Overflow用户

发布于 2016-01-07 21:59:21

我(或其他人,显然)不清楚你到底想要什么,但我认为大致是这样的:

  1. ts_A中查找具有ts_B中的所有NA's;
  2. find行/年的行/年,这些行/年也仅为ts_A中的所有ts_B行/年,其中ts_B中的相同行/年不是ts_B

如果是这样,那么像这样的东西就会起作用。

代码语言:javascript
复制
# make some dummy data
set.seed(123)
ts_A <- data.frame(seq(2000,2014),matrix(rpois(180,450),ncol=12))
colnames(ts_A) <- c("year",month.abb)
ts_B <- data.frame(year=seq(2000,2014),value=apply(ts_A[,-1],1,sum))

# pretend N years in ts_A have NA for all months
N <- 3
knock_out <- sample(ts_A$year,N)
ts_A[ts_A$year %in% knock_out,-1] <- rep(NA,12)
# make one of the years in ts_B also NA
ts_B[ts_B$year %in% knock_out[1],-1] <- NA

# these are the rows/years in ts_A that either have data or a non-NA in ts_B
ts_C <- ts_A[!(is.na(ts_B$value) & apply(is.na(ts_A[,-1]),1,all)),]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34018332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档