0

Is there a way to rename a data frame using the value of a string?

I'm trying to write a function in [R] that takes a vector of values, operates over them, and (among other things) returns a data frame to the global environment using the <<- operator. I'd like the name of this data frame to reflect the name of the vector used to create it.

Ideally the code would look something like this:

my.func <- function(vector.in){ # ... Operations over vector.in ... # data.out <- data.frame(x1 = ...) new.name <- paste("data.out", deparse(substitute(vector.in)), sep="_") #Change the name of data.out to the value of the string new.name changename(data.out, new.name) #Export the newly named object to the global environment new.name <<- new.name # ... More operations ... # } 
3
  • What do you mean by the name of the data frame, the name of a variable it is assigned to? Why not just return the data frame from the function? I can't find documentation for your changename function, ?changename doesn't return anything for me. Commented May 6, 2015 at 2:01
  • I'm curious though, what's the common use case for this sort of thing? Commented May 6, 2015 at 2:36
  • The function I'm writing is designed to recode categorical data in a very specific way, and to return a formula object that can be included in a call to glm(). I'd like to be able to simply point to an existing vector of data and run a function that both (a) creates a "matrix" of coded predictors, and (b) provides a compact way to include the resulting variables (sometimes upwards of 200 of them) into a regression equation. The critical problem is that I need to do this for several variables simultaneously, and thus need my "matricies" to automatically be assigned different names. Commented May 6, 2015 at 3:27

2 Answers 2

1

This is probably not the forum for pure coding questions. But I use the assign function for this type of stuff

# ... Operations over vector.in ... # new.name <- paste("data.out", deparse(substitute(vector.in)), sep="_") assign(new.name,data.frame(x1 = ...)) ls() #you should see new.name which(ls()==new.name) #this will return an integer 

I would not put this inside a function because the assignment will be done inside the environment of that function only. Anyway I am sure there is more formal ways but this works well for me

Sign up to request clarification or add additional context in comments.

4 Comments

Looks like you can pass an environment argument envir to assign, so you can use assign in local scope to make a variable assignment in a global scope.
@MatthewDrury yes I think you are correct, however, I know from experience that environment assignment can get tricky in R, or any language for that matter. In general I prefer not to make assignments across environments if I can avoid it...but that's just my personal preference...and what you mentioned is certainty doable
Agreed. It seems better to just return the data frame than <<-.
Thanks for the quick responses @MatthewDrury. assign() seems to be exactly the function I was looking for. I really appreciate your help.
1

The <<- operator and its brethren are best avoided, but if you must:

myfun <- function(objname){assign(objname,3L,envir=.GlobalEnv)} 

assigns 3 to the object with the name passed. For example,

myfun("gah") gah # [1] 3 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.