lubridate
和data.table
或dplyr
的可能解决方案。
1)data.table:
# load packages
library(lubridate)
library(data.table)
# convert both dataframes to data.table's and add a 'date'-variable
setDT(d1)[, date := as.IDate(sprintf('%s-%02d-01',Year,Month))][]
# idem + substract 3 months with lubridate's '%m-%` function
setDT(d2)[, date := as.IDate(sprintf('%s-%02d-01',Year,Month)) %m-% months(3)][]
# join d1 with d2 and update d1 by reference
d1[d2, on = .(Symbol = Company, date), MeanPrice := MeanPrice][]
其给出:
Symbol Month Year 10KRatio1 10KRatio2 date MeanPrice
1: FLWS 6 2007 100 200 2007-06-01 10.995
2: ACAD 12 2007 500 600 2007-12-01 8.640
一种替代加入法可以是:
d1[d2[, .(Company, date, MeanPrice)], on = .(Symbol = Company, date), nomatch = 0L][]
2)dplyr:
# load packages
library(lubridate)
library(dplyr)
# add a 'date'-variable to 'd1'
# add a 'date'-variable to 'd2' and substract 3 months
# from that with lubridate's '%m-%` function
# select only 'Company', 'date' and 'MeanPrice' from 'd2'
# join 'd1' with 'd2'
d1 %>%
mutate(date = as.Date(sprintf('%s-%02d-01',Year,Month))) %>%
left_join(., d2 %>%
mutate(date = as.Date(sprintf('%s-%02d-01',Year,Month)) %m-% months(3)) %>%
select(Company, date, MeanPrice),
by = c('Symbol' = 'Company', 'date'))
其给出相同的结果:
Symbol Month Year 10KRatio1 10KRatio2 date MeanPrice
1 FLWS 6 2007 100 200 2007-06-01 10.995
2 ACAD 12 2007 500 600 2007-12-01 8.640
使用的数据:
d1 <- structure(list(Symbol = c("FLWS", "ACAD"),
Month = c(6L, 12L),
Year = c(2007L, 2007L),
`10KRatio1` = c(100L, 500L),
`10KRatio2` = c(200L, 600L)),
.Names = c("Symbol", "Month", "Year", "10KRatio1", "10KRatio2"), class = "data.frame", row.names = c(NA, -2L))
d2 <- structure(list(Company = c("FLWS", "FLWS", "FLWS", "ACAD", "ACAD"),
Year = c(2007L, 2007L, 2014L, 2007L, 2008L),
Month = c(1L, 9L, 12L, 1L, 3L),
MeanPrice = c(6.32, 10.995, 17.92, 7.5, 8.64)),
.Names = c("Company", "Year", "Month", "MeanPrice"), class = "data.frame", row.names = c(NA, -5L))