Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in R How do I copy a data frame with a dynamic name into a static named data frame

I have a loop where I change values. I need to take a sequentially named data frame and assign it to a variable:

for(i in 1:n){
    static <- paste('dinamic' , i , sep = '')
    # more code...
}

In other words, I would like the code to resolve to:

static <- dynamic1 # when i = 1, and so forth
like image 804
Illya Avatar asked May 07 '15 01:05

Illya


2 Answers

You can do this using the get function:

for (i in 1:n) {
    static <- get(paste('dinamic' , i , sep = ''))
    # more code...
}

The R documentation about the function.

like image 69
alfakini Avatar answered Oct 19 '22 17:10

alfakini


another way would be to create a empty dataframe and then append your data to with each iteration. Something like below :

    df <- NULL
    for(i in 1:n){
    static <- paste('dinamic' , i , sep = '')
    more code...
    df <- rbind(df,static)}
like image 1
Amrita Sawant Avatar answered Oct 19 '22 18:10

Amrita Sawant