Apply the function to each nth element in the vector
•
Java
I have a vector in R:
> v <- c(5,10,15,20,25,30,35,40,45,50)
I want to apply a function to every Nth element of a vector and let it return a new vector For example, suppose I want to multiply each third element of a vector by 2 Then I'll get
> v [1] 5 10 30 20 25 60 35 40 90 50
I try to extract elements using vector logic:
> v[c(rep(FALSE,2),TRUE)] [1] 15 30 45
I've figured out how to access elements, I can do things for them, but I don't know how to get them back to my original vector v
Solution
We need to allocate
v[c(FALSE,FALSE,TRUE)] <- v[c(FALSE,TRUE)]*2
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码