R: Use the position information of the element when looping the vector

Can I use the index of the element and the element when looping through a vector?

a. Vector < - C ("a", "B", "C", "a", "d") suppose I need the index of 'first' and "a" of a.vector One person cannot use it

Because there are two 'a', it will return to two positions 1 and 4 I need to loop the specific index of the element to be overwritten immediately

I need something like this:

b.vector< -c(“the”,“cat”,“chased”,“mouse”)

for (i in a.vector) {
    element<-b.vector[INDEX.OF(a.vector)])
-------some process using both 'element' and "a"-------}

This looks similar to the 'enumerate' function in Python The solution will be very helpful thank you.

Solution

How do I use index number loops?

for (i in seq_along(a.vector)){
   a.element <- a.vector[i]
   b.element <- b.vector[i]
   ...
}
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
分享
二维码
< <上一篇
下一篇>>