申请任何数据框的一般逻辑:
set.seed(1) # for reproducibility
# create a dataframe frame
df <- as.data.frame(matrix(c(rnorm(10), rnorm(10), rnorm(10),rnorm(10),rnorm(10)), nrow=10))
df # show it
# V1 V2 V3 V4 V5
# 1 -0.6264538 1.51178117 0.91897737 1.35867955 -0.1645236
# 2 0.1836433 0.38984324 0.78213630 -0.10278773 -0.2533617
# ...
# 10 -0.3053884 0.59390132 0.41794156 0.76317575 0.8811077
combinations <- combn(5,3) #123 124 125 ...345
# all combination of any 3 of the 5 columns
lapply(1:dim(combinations)[[2]], function(x) {df[combinations[,x]]})
# sums of all combination of any 3 of the 5 columns
lapply(1:dim(combinations)[[2]], function(x) {rowSums(df[combinations[,x]])})
# use "matrix(unlist(...), nrow)" for better presentation and easier later handlings
matrix(unlist(lapply(1:dim(combinations)[[2]], function(x) {rowSums(df[combinations[,x]])})),nrow=nrow(df))
针对提问者具体数据的解决方法:
mydata <- as.data.frame(matrix(c(
11/2/2015, -0.88, -2.16, -1.04, 1.12, 0.67,
12/1/2015, 1.03, 3.26, -2.25, -5.51, -0.23,
1/4/2016, 1.94, 1.29, 0.13, -1.16, 0.11,
2/1/2016, -0.41, -2.94, 0.99, 3.93, -0.19,
3/1/2016, -0.68, 1.27, -0.79, -2.06, -0.33,
4/1/2016, 1.82, 1.22, -0.05, -1.27, -0.46,
5/2/2016, -0.36, 3.40, 0.63, -2.77, 0.46,
6/1/2016, 1.94, 0.77, 0.51, -0.26, 1.66,
7/1/2016, 0.12, 3.18, 1.84, -1.34, -0.67,
8/1/2016, -1.83, -0.20, -1.10, -0.90, -1.91,
9/1/2016, 0.05, 0.31, 1.11, 0.80, 1.17,
10/3/2016, -0.02, 3.19, -0.81, -4.00, 0.29), nrow=12, byrow=TRUE))
names(mydata) <- c("Date", "DCM", "TMUS", "SKM", "RCI", "SPOK") # name the columns
mydata # show the dataframe
# Date DCM TMUS SKM RCI SPOK
# 1 0.0027295285 -0.88 -2.16 -1.04 1.12 0.67
# 2 0.0059553350 1.03 3.26 -2.25 -5.51 -0.23
# ............................................
# 12 0.0016534392 -0.02 3.19 -0.81 -4.00 0.29
combinations <- combn(5,3) #123 124 125 ...345
# all combination of any 3 of the 5 columns
lapply(1:dim(combinations)[[2]], function(x) {mydata[,2:6][combinations[,x]]})
# sums of all combination of any 3 of the 5 columns
lapply(1:dim(combinations)[[2]], function(x) {rowSums(mydata[,2:6][combinations[,x]])})
# use "matrix(unlist(...), nrow)" for better presentation and easier later handlings
matrix(unlist(lapply(1:dim(combinations)[[2]], function(x) {rowSums(mydata[,2:6][combinations[,x]])})),nrow=nrow(mydata))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] -4.08 -1.92 -2.37 -0.80 -1.25 0.91 -2.08 -2.53 -0.37 0.75
# [2,] 2.04 -1.22 4.06 -6.73 -1.45 -4.71 -4.50 0.78 -2.48 -7.99
# [3,] 3.36 2.07 3.34 0.91 2.18 0.89 0.26 1.53 0.24 -0.92
# ...............................................................
# [12,] 2.36 -0.83 3.46 -4.83 -0.54 -3.73 -1.62 2.67 -0.52 -4.52
正确执行。
检查,例如,第 10 种情况;0.75=sum(-1.04, 1.12, 0.67) -7.99=sum(-2.25, -5.51, -0.23) ...