Skip to content
Prev 157063 / 398506 Next

looping through variables

Perhaps what you want is get().

    apple <- rnorm(5)
    orange <- runif(5)

    fruits <- c("apple", "orange")

    fruit.data <- NULL
    for( fruit in fruits ){
       v <- get(fruit)
       fruit.data <- cbind(fruit.data, v)
    }
    colnames(fruit.data) <- fruits

    fruit.data

Here the resulting output is a matrix which works if all of your inputs 
have the same length. If they don't, then you probably want to use a 
list instead. Also have a look at assign().

Regards, Adai
K. Fleischer wrote: