0

我很难从 R 中的两个不同数据源获取具有相同日期的值。代码是

  #Monthly data
  month_data <- c(580.11, 618.25, 641.24, 604.85, 580.86, 580.07, 632.97, 
                  685.09, 754.50, 680.30, 698.37, 707.38, 480.11, 528.25,
                  541.24, 614.85, 680.86)

  month_dates <- seq(as.Date("2001/06/01"), by = "1 months", length = 17) 

  month_data <- data.frame(month_dates, month_data)

  #the dates_for_match is a list: 

dates_for_match<-list(c( "2001-08-01","2001-09-01", "2001-10-01"),c("2001-11-01","2001-12-01","2002-01-01"),c("2002-02-01","2002-03-01","2002-04-01"),c("2002-05-01","2002-06-01","2002-07-01"),c( "2002-08-01","2002-09-01", "2002-10-01"))

例子:

  > dates_for_match
[[1]]
[1] "2001-08-01" "2001-09-01" "2001-10-01"

[[2]]
[1] "2001-11-01" "2001-12-01" "2002-01-01"

[[3]]
[1] "2002-02-01" "2002-03-01" "2002-04-01"

[[4]]
[1] "2002-05-01" "2002-06-01" "2002-07-01"

[[5]]
[1] "2002-08-01" "2002-09-01" "2002-10-01"

我想使用dates_for_match列表从 month_data 中获取具有相同日期的值。

4

1 回答 1

2

你需要%in%...

month_data[ month_dates %in% unlist( dates_for_match ) , 2 ]
于 2013-08-09T18:15:12.340 回答