[ACCEPTED]-R: Using position information of elements when looping through a vector. -indexing

Accepted answer
Score: 15

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]
   ...
}

0

Score: 1

Use which.max instead of which. It will pick out the position 19 of the first TRUE since TRUE > FALSE.

 which.max(a.vector=="a")
#[1] 1

It's 18 possible that @James understood your request 17 better than I. You really asked a different 16 question at the end of your text than you 15 asked in the subject line so you might wnat 14 to clarify. I will add that the notion of 13 passing the location of "i" in a hidden 12 form along with its value is rather foreign 11 to R. People often ask whether R is "pass 10 by value" versus "pass by reference". The 9 correct answer is neither... that it is 8 "pass by promise". However, that is conceptually 7 a lot closer to "pass by value" than it 6 would be to "pass by reference". for is a function 5 and R makes a copy of the arguments being 4 passed from the function invocation into 3 its body. There is no "location" information 2 that gets carried along unless such information 1 is what you do in fact asked it to pass.

More Related questions