Reputation: 1981
I have 2 vectors x
and w
. I want to extract the elements of vector x
according to order of w
and the lengths z
. How can I do this easier and faster in R?
x = c(1.3, 1.5, 1.6, 2.4, 5.3, 6.6, 7.8)
w = c(1, 2, 3, 5, 7, 4, 6)
z = 5
m = matrix(c(x[w], w), ncol=2)
> m
[,1] [,2]
[1,] 1.3 1
[2,] 1.5 2
[3,] 1.6 3
[4,] 5.3 5
[5,] 7.8 7
[6,] 2.4 4
[7,] 6.6 6
f = m[1:z,1]
> f
[1] 1.3 1.5 1.6 5.3 7.8
Upvotes: 0
Views: 132