Is there any pattern matching approach for looping through a list of variables in R?
I’m quite aware of using indices, but, so far, I’ve found none on multiple attributions in a loop.
What I have is this:
x <- c(1, 2, 3, 4)
y <- c(4, 3, 2, 1)
for (i in 1:length(x)) {
x[i]
y[i]
}
and I would like to have:
x <- c(1, 2, 3, 4)
y <- c(4, 3, 2, 1)
for ((xi, yi) in c(x, y)) {
xi
yi
}
Regards!
No, R doesn’t let you unpack values in this way. You can only assign each element in the sequence to a single variable.
See here:
http://cran.r-project.org/doc/manuals/R-lang.html#for