r - How to create a list of data.frames with data that has groups of unequal rows -
i'm importing spreadsheet r xlconnect (dput below), , it's grouped like:
eur date eur.high|eur.low|eur.open|eur.last|jpy.date jpy.high jpy.low jpy.open jpy.close|gbp.date|gbp.high|gbp.low|gbp.open|gbp.last 4/5/2017 1.0689 1.0635 1.0674 1.0663 4/5/2017 111.45 110.54 110.74 110.7 4/7/2017 1.2478 1.2371 1.247 1.238 4/4/2017 1.0677 1.0636 1.067 1.0674 4/4/2017 110.94 110.27 110.89 110.74 4/6/2017 1.25 1.245 1.2483 1.247 4/3/2017 1.0681 1.0643 1.0665 1.067 4/3/2017 111.59 110.86 111.4 110.9 4/5/2017 1.25 1.2423 1.244 1.2483 3/31/2017 1.0702 1.0652 1.0674 1.0652 4/2/2017 112.2 111.25 111.92 111.39 4/4/2017 1.25 1.2419 1.2486 1.244 3/30/2017 1.077 1.0672 1.0766 1.0674 4/1/2017 112.2 111.25 111.92 111.39 4/3/2017 1.26 1.2466 1.2548 1.2486 3/29/2017 1.0827 1.074 1.0813 1.0766 3/31/2017 112.2 111.25 111.92 111.39 3/31/2017 1.26 1.2434 1.2469 1.255 3/28/2017 1.0873 1.0799 1.0864 1.0814 3/30/2017 111.94 110.94 111.04 111.92 3/27/2017 1.0906 1.0795 1.0798 1.0864 3/29/2017 111.32 110.72 111.14 111.04 3/24/2017 1.0818 1.076 1.0783 1.0798 3/28/2017 111.2 110.18 110.66 111.15 3/23/2017 1.0805 1.0768 1.0797 1.0783 3/27/2017 111.3 110.11 111.24 110.66 3/22/2017 1.0825 1.0776 1.0811 1.0797 3/26/2017 111.48 110.63 110.94 111.34 so each variable (i.e eur, jpy, gbp) has same number of rows respective high,low,open close prices. however, there different rows between variables (i.e: eur , gbp have different rows).
previously using code
#securities cur<-c('eur', 'jpy', 'gbp') #extracts columns corresponds each security, sorts list of dataframes ii = sapply(cur, function(y) grep(paste0("^", y), names(dat))) mergeddata = lapply(as.data.frame(ii), function(x) dat[x]) but error (what assume be) as.data.frame saying
error in (function (..., row.names = null, check.rows = false, check.names = true, : arguments imply differing number of rows: 5, 0 sample dput attached below:
structure(list(eur.date = structure(c(1491368400, 1491282000, 1491195600, 1490936400, 1490850000, 1490763600, 1490677200, 1490590800, 1490331600, 1490245200, 1490158800), tzone = "", class = c("posixct", "posixt")), eur.high = c(1.0689, 1.0677, 1.0681, 1.0702, 1.077, 1.0827, 1.0873, 1.0906, 1.0818, 1.0805, 1.0825), eur.low = c(1.0635, 1.0636, 1.0643, 1.0652, 1.0672, 1.074, 1.0799, 1.0795, 1.076, 1.0768, 1.0776), eur.open = c(1.0674, 1.067, 1.0665, 1.0674, 1.0766, 1.0813, 1.0864, 1.0798, 1.0783, 1.0797, 1.0811), eur.last = c(1.0663, 1.0674, 1.067, 1.0652, 1.0674, 1.0766, 1.0814, 1.0864, 1.0798, 1.0783, 1.0797), jpy.date = structure(c(1491368400, 1491282000, 1491195600, 1491109200, 1491022800, 1490936400, 1490850000, 1490763600, 1490677200, 1490590800, 1490504400), tzone = "", class = c("posixct", "posixt")), jpy.high = c(111.45, 110.94, 111.59, 112.2, 112.2, 112.2, 111.94, 111.32, 111.2, 111.3, 111.48), jpy.low = c(110.54, 110.27, 110.86, 111.25, 111.25, 111.25, 110.94, 110.72, 110.18, 110.11, 110.63), jpy.open = c(110.74, 110.89, 111.4, 111.92, 111.92, 111.92, 111.04, 111.14, 110.66, 111.24, 110.94), jpy.close = c(110.7, 110.74, 110.9, 111.39, 111.39, 111.39, 111.92, 111.04, 111.15, 110.66, 111.34), gbp.date = structure(c(1491541200, 1491454800, 1491368400, 1491282000, 1491195600, 1490936400, na, na, na, na, na), tzone = "", class = c("posixct", "posixt")), gbp.high = c(1.2478, 1.2506, 1.2498, 1.2496, 1.2567, 1.2558, na, na, na, na, na), gbp.low = c(1.2371, 1.245, 1.2423, 1.2419, 1.2466, 1.2434, na, na, na, na, na), gbp.open = c(1.247, 1.2483, 1.244, 1.2486, 1.2548, 1.2469, na, na, na, na, na), gbp.last = c(1.238, 1.247, 1.2483, 1.244, 1.2486, 1.255, na, na, na, na, na)), .names = c("eur.date", "eur.high", "eur.low", "eur.open", "eur.last", "jpy.date", "jpy.high", "jpy.low", "jpy.open", "jpy.close", "gbp.date", "gbp.high", "gbp.low", "gbp.open", "gbp.last"), row.names = c(na, -11l), class = "data.frame")
Comments
Post a Comment