list 组合(cbind)不同长度的向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5531471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:37:34  来源:igfitidea点击:

Combining (cbind) vectors of different length

listrmergematrix

提问by Nick

I have several vectors of unequal length and I would like to cbindthem. I've put the vectors into a list and I have tried to combine the using do.call(cbind, ...):

我有几个长度不等的向量,我想要cbind它们。我已将向量放入列表中,并尝试结合使用do.call(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)

As expected, the number of rows in the resulting matrix is the length of the longest vector, and the values of the shorter vectors are recycled to make up for the length.

正如预期的那样,结果矩阵中的行数是最长向量的长度,较短向量的值被回收以弥补长度。

Instead I'd like to pad the shorter vectors with NAvalues to obtain the same length as the longest vector. I'd like the matrix to look like this:

相反,我想用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

How can I go about doing this?

我该怎么做呢?

回答by Sacha Epskamp

You can use indexing, if you index a number beyond the size of the object it returns NA. This works for any arbitrary number of rows defined with foo:

您可以使用索引,如果您索引的数字超出它返回的对象的大小NA。这适用于定义为任意数量的行foo

nm <- list(1:8,3:8,1:5)

foo <- 8

sapply(nm, '[', 1:foo)

EDIT:

编辑:

Or in one line using the largest vector as number of rows:

或者在一行中使用最大的向量作为行数:

sapply(nm, '[', seq(max(sapply(nm,length))))

From R 3.2.0you may use lengths("get the length of each element of a list") instead of sapply(nm, length):

R 3.2.0您可以使用lengths(“获取列表中的每个元素的长度”),而不是sapply(nm, length)

sapply(nm, '[', seq(max(lengths(nm))))

回答by Wojciech Sobala

You should fill vectors with NA before calling do.call.

你应该在调用 do.call 之前用 NA 填充向量。

nm <- list(1:8,3:8,1:5)

max_length <- max(unlist(lapply(nm,length)))
nm_filled <- lapply(nm,function(x) {ans <- rep(NA,length=max_length);
                                    ans[1:length(x)]<- x;
                                    return(ans)})
do.call(cbind,nm_filled)

回答by Thierry

This is a shorter version of Wojciech's solution.

这是 Wojciech 解决方案的较短版本。

nm <- list(1:8,3:8,1:5)
max_length <- max(sapply(nm,length))
sapply(nm, function(x){
    c(x, rep(NA, max_length - length(x)))
})

回答by Ronak Shah

Late to the party but you could use cbind.fillfrom rowrpackage with fill = NA

聚会迟到,但您可以使用cbind.fillfrom rowrpackage withfill = NA

library(rowr)
do.call(cbind.fill, c(nm, fill = NA))

#  object object object
#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

If you have a named listinstead and want to maintain the headers you could use setNames

如果您有一个命名的list并且想要维护您可以使用的标题setNames

nm <- list(a = 1:8, b = 3:8, c = 1:5)
setNames(do.call(cbind.fill, c(nm, fill = NA)), names(nm))

#  a  b  c
#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

回答by akrun

Here is an option using stri_list2matrixfrom stringi

这是一个使用stri_list2matrixfrom的选项stringi

library(stringi)
out <- stri_list2matrix(nm)
class(out) <- 'numeric'
out
#      [,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