When looping through a vector, is it possible to use the index of an element along with the element?
a.vector<-c("a", "b", "c", "a", "d")
Let's suppose I need the index of the 'first' "a" of a.vector. One can't use
which(a.vector == "a")
Because there are two 'a' s and it would return two positions 1 and 4. I need the specific index of the element which the loop is instantly covering.
I need it for something like this:
b.vector<-c("the", "cat", "chased", "a", "mouse")
for (i in a.vector) {
element<-b.vector[INDEX.OF(a.vector)])
-------some process using both 'element' and "a"-------}
This seems similar to the 'enumerate' function in python. A solution would help a lot. Thanks.
How about just looping with the index number?
for (i in seq_along(a.vector)){
a.element <- a.vector[i]
b.element <- b.vector[i]
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With