I am writing a function in R and have an input id
that is a numeric vector.
Can I write a for loop that states:
for(id in id)
{
/****/
}
and loop over the id
vector with the variable id
or will this cause problem?
It will produce the expected result but it will destroy the id
to the right of the in
. Also within the loop all references to id
will be the id
to the left of in
so you won't be able to reference the one on the right.
> id <- 1:3
> for(id in id) print(id)
[1] 1
[1] 2
[1] 3
> id
[1] 3
Try writing it like this to be safer:
ids <- 1:3
for(id in ids) print(id)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments