r - Find all the sums of all combinations of 3 rows from 5 columns -
i've loaded table .cvs file using
mydata = read.csv("cs2data.csv") # read csv file which gave me:
mydata date dcm tmus skm rci spok 1 11/2/2015 -0.88 -2.16 -1.04 1.12 0.67 2 12/1/2015 1.03 3.26 -2.25 -5.51 -0.23 3 1/4/2016 1.94 1.29 0.13 -1.16 0.11 4 2/1/2016 -0.41 -2.94 0.99 3.93 -0.19 5 3/1/2016 -0.68 1.27 -0.79 -2.06 -0.33 6 4/1/2016 1.82 1.22 -0.05 -1.27 -0.46 7 5/2/2016 -0.36 3.40 0.63 -2.77 0.46 8 6/1/2016 1.94 0.77 0.51 -0.26 1.66 9 7/1/2016 0.12 3.18 1.84 -1.34 -0.67 10 8/1/2016 -1.83 -0.20 -1.10 -0.90 -1.91 11 9/1/2016 0.05 0.31 1.11 0.80 1.17 12 10/3/2016 -0.02 3.19 -0.81 -4.00 0.29 i'd find combination of 3 of 5 numbers each month (row). tried using combn function based on answer found here:
combin <- combn(mydata, 3, rowsums, simplify = true) but gave me error-
"error in fun(x[a], ...) : 'x' must numeric"
next tried naming each column separately
dcm=mydata[2] tmus=mydata[3] skm=mydata[4] rci=mydata[5] spok=mydata[6] and using:
stock_ret <- data.table(dcm, tmus,skm,rci,spok) combin <- combn(stock_ret, 3, rowsums, simplify = true) i suspect there's easier way use column headers directly .cvs file i'm stuck.
get first column dates (origin of error in question):
mydata <- mydata[,-1] use combn calculate selecting 3 columns @ time:
combn(mydata, m = 3, fun = rowsums, simplify = true) example:
> mydata <- iris[1:10,1:4] > combn(mydata, m = 3, fun = rowsums, simplify = true) [,1] [,2] [,3] [,4] [1,] 10.0 8.8 6.7 5.1 [2,] 9.3 8.1 6.5 4.6 [3,] 9.2 8.1 6.2 4.7 [4,] 9.2 7.9 6.3 4.8 [5,] 10.0 8.8 6.6 5.2 [6,] 11.0 9.7 7.5 6.0 [7,] 9.4 8.3 6.3 5.1 [8,] 9.9 8.6 6.7 5.1 [9,] 8.7 7.5 6.0 4.5 [10,] 9.5 8.1 6.5 4.7
Comments
Post a Comment