![](/img/trans.png)
[英]Taking the last element of the vectors from a list and create a new vector out of it
[英]Create a new vector formed by a list of vectors
假設我有m個向量: a_1 = (a_{11}...a_{1n}) ... a_m = (a_{m1}...a_{mn})
我想要一個長度為mn的新向量b
b = (a_{11}...a_{m1} a_{12}...a_{m2}...a_{1n}...a_{mn})
我可以想到一個for循環:
>a<-c(1,1,1);b<-c(2,2,2);c<-c(3,3,3)
>x<-NULL
>for (i in 1:3) {x<-c(x,c(a[i],b[i],c[i]))}
>x
[1] 1 2 3 1 2 3 1 2 3
有沒有更好的辦法?
或使用mapply
...
c( mapply( c , a , b , c ) )
[1] 1 2 3 1 2 3 1 2 3
c(matrix(c(a, b, c), nrow=length(a), byrow=TRUE))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.