Christine Blume
Christine Blume

Reputation: 527

R generate file names in loop

I am iterating through a loop and with every step I would like to give the generated data a different name and save it to the workspace.

    for (l in 1:c){
      data_daily <- data_temp_daily[l,]
      data_daily_time <- data[(l:288*l),2]
      newfile <- paste("data_daily_day", l, sep = "") 
      newfile <- data.frame(data_daily_time, data_daily)
    }

newfile is the filename, which is correctly generated. However, I would like to save my df to the workspace with the newfile name! How can that be accomplished?

THANKS!

Upvotes: 1

Views: 970

Answers (2)

arodrisa
arodrisa

Reputation: 300

You have the answer to this question here: Dynamic Variable naming in r

You should google a bit before asking

Upvotes: 0

giac
giac

Reputation: 4299

Please provide more info about your original datasets.

Let us imagine you want to use the mtcars data. You can easily assign rows or columns to your local environmment with

c = 5 
for (l in 1:c){
  assign( x = paste("data_daily_day", l, sep = ""), value = mtcars[l,] )
}

With assign you paste the name with x = and the data or values with value =. Here we are assigning each rows to our environment.

I can help you further because I dont know what is data_temp_daily and data. Hope this help.

Upvotes: 1

Related Questions