Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save() with a particular variable name

Tags:

r

I am repeatedly applying a function to read and process a bunch of csv files. Each time it runs, the function creates a data frame (this.csv.data) and uses save() to write it to a .RData file with a unique name. Problem is, later when I read these .RData files using load(), the loaded variable names are not unique, because each one loads with the name this.csv.data....

I'd like to save them with unique tags so that they come out properly named when I load() them. I've created the following code to illustrate .

this.csv.data = list(data=c(1:9), unique_tag = "some_unique_tag") assign(this.csv.data$unique_tag,this.csv.data$data) # I want to save the data,  # with variable name of <unique_tag>,  # at a file named <unique_tag>.dat saved_file_name <- paste(this.csv.data$unique_tag,"RData",sep=".") save(get(this.csv.data$unique_tag), saved_file_name) 

but the last line returns:

"Error in save(get(this_unique_tag), file = data_tag) :    object ‘get(this_unique_tag)’ not found" 

even though the following returns the data just fine:

get(this.csv.data$unique_tag) 
like image 443
J. Win. Avatar asked Jan 13 '11 00:01

J. Win.


People also ask

How do I save a variable name in Matlab?

save( filename ) saves all variables from the current workspace in a MATLAB® formatted binary file (MAT-file) called filename . If filename exists, save overwrites the file. save( filename , variables ) saves only the variables or fields of a structure array specified by variables .

Can you reuse variable names in different functions?

Therefore, in your case, the variables will be passed from the main driver to the individual functions by reference. So, yes, you can have variables of the same name in different scopes.


1 Answers

Just name the arguments you use. With your code the following works fine:

save(list = this.csv.data$unique_tag, file=saved_file_name) 
like image 200
daroczig Avatar answered Oct 02 '22 18:10

daroczig