我有幾個向量長度不等,我想cbind
他們。我已經把載體放入一個列表,我試圖結合使用do.call(cbind, ...)
:不同長度的組合(cbind)載體
nm <- list(1:8, 3:8, 1:5)
do.call(cbind, nm)
# [,1] [,2] [,3]
# [1,] 1 3 1
# [2,] 2 4 2
# [3,] 3 5 3
# [4,] 4 6 4
# [5,] 5 7 5
# [6,] 6 8 1
# [7,] 7 3 2
# [8,] 8 4 3
# Warning message:
# In (function (..., deparse.level = 1) :
# number of rows of result is not a multiple of vector length (arg 2)
正如預期的那樣,在產生的矩陣的行數是最長的向量的長度,和的值較短的載體被回收以彌補長度。
相反,我想用NA
值填充較短的向量,以獲得與最長向量相同的長度。我想矩陣看起來像這樣:
# [,1] [,2] [,3]
# [1,] 1 3 1
# [2,] 2 4 2
# [3,] 3 5 3
# [4,] 4 6 4
# [5,] 5 7 5
# [6,] 6 8 NA
# [7,] 7 NA NA
# [8,] 8 NA NA
我該如何去做這件事?
閃光的光輝:nm < - cbind(z1, c(z2,rep(NA,length(z1)-length(z2))) ) – Nick 2011-04-03 18:17:40