lmerge: Merge list of data.frames into data.table by key column

Description Usage Arguments Examples

View source: R/lmerge.R

Description

Merge list of data.frames into data.table by key column

Usage

1
lmerge(x, key, value, na.omit = T)

Arguments

x

named list of data.frames

key

column name to merge by

value

column name of value variable

na.omit

should leading NA values be omitted?

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from = '1990-01-01'
to = '2016-08-30'

symbols = fread( '
               symbol, comment
               EFA, iShares MSCI EAFE Index Fund
               VTI, Vanguard Total Stock Market
               TLT, iShares 20+ Year Treasury Bond
               RWX, SPDR Dow Jones International RelEst
               IEV, iShares Europe
               IEF, iShares 7-10 Year Treasury Bond
               ICF, iShares Cohen & Steers Realty Maj.
               GLD, SPDR Gold Shares
               EWJ, iShares MSCI Japan
               EEM, iShares MSCI Emerging Markets
               DBC, PowerShares DB Commodity Tracking' )

# download historical market data
prices_list = lapply_named( symbols$'symbol', get_yahoo_data, from, to )

# table of close prices
prices = lmerge( prices_list, 'date' , 'close' )

# calculate returns and performance
dates = prices[, date ]
prices[, date := NULL ]
returns = lapply( prices, returns ) %>% setDT
performance = lapply( returns + 1, cumprod ) %>% setDT

# plot historical values
plot_ts( data.table( dates, returns ), legend = 'topleft' )
plot_ts( data.table( dates, prices ), legend = 'topleft' )
plot_ts( data.table( dates, performance ), legend = 'topleft' )

QuantTools documentation built on Oct. 23, 2020, 7:54 p.m.