Splitting Matrix into multiple couples of columns in R -
using r have 10 rows x 6 columns matrix. need split submatrices throuhg gruoping couples of columns no overlapping.
i.e. matrix has columns a,b,c,d,e,f , need extract 3 different matrices (or data.frames or whatever object within financial packages zoo or timeseries) formed columns ab, cd , ef.
ps: matrix contains financial data series , couple of columns has date columns , nav column
using dummy data (note must have dataframe otherwise r not allow hold date , numeric values in matrix [unless converted characters or raw numeric representations])
set.seed(42) df <- data.frame(a = sys.date() + 0:9, b = rnorm(10), c = sys.date() - 0:9, d = rnorm(10), e = sys.date() - 20:29, f = rnorm(10)) > head(df) b c d e f 1 2013-04-05 1.3709584 2013-04-05 1.3048697 2013-03-16 -0.3066386 2 2013-04-06 -0.5646982 2013-04-04 2.2866454 2013-03-15 -1.7813084 3 2013-04-07 0.3631284 2013-04-03 -1.3888607 2013-03-14 -0.1719174 4 2013-04-08 0.6328626 2013-04-02 -0.2787888 2013-03-13 1.2146747 5 2013-04-09 0.4042683 2013-04-01 -0.1333213 2013-03-12 1.8951935 6 2013-04-10 -0.1061245 2013-03-31 0.6359504 2013-03-11 -0.4304691
one easy way form index columns want - here chose first column of each pair, 1, 3, 5, etc.
start <- seq(1, = 2, length = ncol(df) / 2)
then, lapply
on indices in start
, select our data frame i
th , i
th + 1
columns i
takes each index start
in turn (df[i:(i+1)]
)
sdf <- lapply(start, function(i, df) df[i:(i+1)], df = df)
which gives:
> sdf [[1]] b 1 2013-04-05 1.37095845 2 2013-04-06 -0.56469817 3 2013-04-07 0.36312841 4 2013-04-08 0.63286260 5 2013-04-09 0.40426832 6 2013-04-10 -0.10612452 7 2013-04-11 1.51152200 8 2013-04-12 -0.09465904 9 2013-04-13 2.01842371 10 2013-04-14 -0.06271410 [[2]] c d 1 2013-04-05 1.3048697 2 2013-04-04 2.2866454 .... > str(sdf) list of 3 $ :'data.frame': 10 obs. of 2 variables: ..$ a: date[1:10], format: "2013-04-05" "2013-04-06" ... ..$ b: num [1:10] 1.371 -0.565 0.363 0.633 0.404 ... $ :'data.frame': 10 obs. of 2 variables: ..$ c: date[1:10], format: "2013-04-05" "2013-04-04" ... ..$ d: num [1:10] 1.305 2.287 -1.389 -0.279 -0.133 ... $ :'data.frame': 10 obs. of 2 variables: ..$ e: date[1:10], format: "2013-03-16" "2013-03-15" ... ..$ f: num [1:10] -0.307 -1.781 -0.172 1.215 1.895 ...
an advantage of keeping sub-data frames in list can apply function or other operation sub-data frames using loop or tools lapply
or sapply
example.
Comments
Post a Comment