我有几个2D向量,其中第一列是索引(例如“timestamp”),第二列是值。
在每个for循环中,我得到其中一个向量,我想将它们全部合并到一个大的2D矩阵中,其中第一列是索引,而另一列是这些原始向量中的不同值。
例如:
v1 <- matrix(c(seq(1:10), rnorm(10)), nrow = 10, ncol = 2)
v2 <- matrix(c(1, 2, 3, 6, 7, 8, 9, rnorm(7)), nrow = 7, ncol = 2)
v3 <- matrix(c(11, 12, rnorm(2)), nrow = 2, ncol = 2)
我想要的结果是12x4矩阵:
第一列是1:12,第二列是v1的值,根据时间戳1:10,第三列是v2的值仅在1,2,3,6,7,8,9,第四列column是仅在位置11,12中的v3的值。 如果没有可用的值,则将放置NULL。
请注意,我希望根据时间戳合并所有内容。
答案 0 :(得分:2)
如果生成的所有“向量”都被称为“vi”,我是一个数字,你可以这样做:
# get the names of all the "vectors" generated :
list_vec<-ls(pattern="^v\\d+$")
# get all unique timestamps (all unique values from 1st column of the different "vectors")
unique_timestamp<-unique(unlist(sapply(list_vec,function(x){get(x)[,1]})))
# create the matrix that will contain all results :
new_mat<-matrix(,nrow=length(unique_timestamp),ncol=length(list_vec)+1)
new_mat[,1]<-sort(unique_timestamp)
colnames(new_mat)<-c("timestamp",list_vec)
# finally, fill the matrix with the values in second column of the different "vectors", with respect to the timestamps
new_mat[,2:ncol(new_mat)]<-sapply(list_vec,function(x,mat){
x<-get(x)
x[match(mat[,1],x[,1]),2]
},new_mat)
> new_mat
timestamp v1 v2 v3
[1,] 1 -0.95467687 -1.2764675 NA
[2,] 2 -0.82596352 0.8011679 NA
[3,] 3 0.20617686 0.3820669 NA
[4,] 4 -0.09122235 NA NA
[5,] 5 0.42571662 NA NA
[6,] 6 -0.11503517 1.2128891 NA
[7,] 7 0.64854445 0.4053852 NA
[8,] 8 0.22632685 0.7690795 NA
[9,] 9 -1.52236147 0.3290537 NA
[10,] 10 0.19791912 NA NA
[11,] 11 NA NA -2.0296883
[12,] 12 NA NA 0.1624292
答案 1 :(得分:1)
假设你总是有三个向量:
M <- matrix(NA, 12, 4)
M[,1] <- 1:12 # Fill first column
M[v1[,1],2] <- v1[,2] # Fill second column
M[v2[,1],3] <- v2[,2] # Fill third column
M[v3[,1],4] <- v3[,2] # Fill fourth column
这应该很容易推广到任意维度。
答案 2 :(得分:1)
我希望这会对你有所帮助
http://www.inside-r.org/packages/cran/qpcR/docs/cbind.na
或以下是一个例子
library(plyr)
>> x
> [1] 1 2 3 4 5 6
>> y
> [1] 34 5 6
t(rbind.fill.matrix(matrix(x,nrow = 1),matrix(y,nrow = 1)))
[,1] [,2]
1 1 34
2 2 5
3 3 6
4 4 NA
5 5 NA
6 6 NA
答案 3 :(得分:1)
你可以做到
lst <- mget(ls(pattern='^v\\d+'))
Un <- sort(unique(unlist(lapply(lst,`[`, ,1 ))))
cbind(timestamp=Un,sapply(lst, function(x)
ifelse(Un %in% x[,1], x[,2], NA)))
# timestamp v1 v2 v3
# [1,] 1 -0.21183360 -1.7427876 NA
# [2,] 2 -1.04159113 -1.3249530 NA
# [3,] 3 -1.15330756 -0.5479339 NA
# [4,] 4 0.32153150 NA NA
# [5,] 5 -1.50012988 NA NA
# [6,] 6 -0.44553326 0.9275789 NA
# [7,] 7 1.73404543 -0.7167693 NA
# [8,] 8 0.51129562 -1.7427876 NA
# [9,] 9 0.09964504 -1.3249530 NA
# [10,] 10 -0.05789111 NA NA
# [11,] 11 NA NA 0.9623997
# [12,] 12 NA NA 1.5458846
set.seed(25)
v1 <- matrix(c(seq(1:10), rnorm(10)), nrow=10, ncol=2)
v2 <- cbind(c(1,2,3,6,7,8,9), rnorm(7))
v3 <- cbind(11:12, rnorm(2))
答案 4 :(得分:0)
这是另一种选择,这次使用来自“reshape2”的melt
和dcast
:
library(reshape2)
dcast(
melt(lapply(mget(ls(pattern='^v\\d+')), as.data.frame), id.vars = "V1"),
V1 ~ L1, value.var = "value")
# V1 v1 v2 v3
# 1 1 -0.6264538 1.51178117 NA
# 2 2 0.1836433 0.38984324 NA
# 3 3 -0.8356286 -0.62124058 NA
# 4 4 1.5952808 NA NA
# 5 5 0.3295078 NA NA
# 6 6 -0.8204684 -2.21469989 NA
# 7 7 0.4874291 1.12493092 NA
# 8 8 0.7383247 -0.04493361 NA
# 9 9 0.5757814 -0.01619026 NA
# 10 10 -0.3053884 NA NA
# 11 11 NA NA 0.9438362
# 12 12 NA NA 0.8212212