我有一个矩阵列表,在列表元素中,有些列是冗余的,我想将该列表简化为单个矩阵,只保留唯一列。
以下是一个示例数据:
FileStream
我正在寻找类似的东西:
set.seed(1)
#starting matrix
mat <- matrix(rnorm(10*6),nrow=10,dimnames=list(NULL,c("a_1","a_2","b_1","b_2","c_1","c_2")))
mat.list <- list(mat[,c(1,2,3,4)],mat[,c(1,2,5,6)],mat[,c(3,4,5,6)])
但是这将保留唯一列(在此示例中,完全返回res.mat <- Reduce(cbind,mat.list)
)
答案 0 :(得分:0)
Reduce(function(x,y)cbind(x,y[,!colnames(y)%in%colnames(x)]),mat.list)
a_1 a_2 b_1 b_2 c_1 c_2
[1,] -0.6264538 1.51178117 0.91897737 1.35867955 -0.1645236 0.3981059
[2,] 0.1836433 0.38984324 0.78213630 -0.10278773 -0.2533617 -0.6120264
[3,] -0.8356286 -0.62124058 0.07456498 0.38767161 0.6969634 0.3411197
[4,] 1.5952808 -2.21469989 -1.98935170 -0.05380504 0.5566632 -1.1293631
[5,] 0.3295078 1.12493092 0.61982575 -1.37705956 -0.6887557 1.4330237
[6,] -0.8204684 -0.04493361 -0.05612874 -0.41499456 -0.7074952 1.9803999
[7,] 0.4874291 -0.01619026 -0.15579551 -0.39428995 0.3645820 -0.3672215
[8,] 0.7383247 0.94383621 -1.47075238 -0.05931340 0.7685329 -1.0441346
[9,] 0.5757814 0.82122120 -0.47815006 1.10002537 -0.1123462 0.5697196
[10,] -0.3053884 0.59390132 0.41794156 0.76317575 0.8811077 -0.1350546