Make two vectors of different lengths equal in length

杀马特。学长 韩版系。学妹 提交于 2021-02-10 13:17:09

问题


I have two vectors of different lenghts. How can I start both series so their ends concide.

x<-c(1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6)
y<-c(1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4)

I do it with the code below but I guess there must be a more elegant way

x<-x[((length(x)-length(y))+1):length(x)]

x
 [1]  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6
y
 [1]  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4

回答1:


Use tail, and min to determine the shortest vector:

shortest <- min(length(x), length(y))
y <- tail(y, shortest)
x <- tail(x, shortest)


来源:https://stackoverflow.com/questions/21217849/make-two-vectors-of-different-lengths-equal-in-length

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!