我有一个从1902年到2014年的时间序列A (ts_A),frequency=12,每个月的值和某一年(以及随后的月份)的NA值。
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 (年分布)
year value
1902 6524
1903 5682
.... ....
2014 5984只有在ts_B中存在该年的值时,我才会使用ts_A中的数据NA,因为稍后我会用一个比例来替换它们。
发布于 2016-01-07 21:59:21
我(或其他人,显然)不清楚你到底想要什么,但我认为大致是这样的:
ts_A中查找具有ts_B中的所有NA's;ts_A中的所有ts_B行/年,其中ts_B中的相同行/年不是ts_B如果是这样,那么像这样的东西就会起作用。
# 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)),]https://stackoverflow.com/questions/34018332
复制相似问题