Take the 2-minute tour ×

For a function that I am writing, the output is a dataframe. But how do i assign the values that are in one of the columns of my dataframe to objects?

For example, if I have 2 vectors that I cbind into a dataframe

>numbers<-c(33, 44, 55, 66)
>names<-c("A", "B", "C", "D")
>MYdataframe<-data.frame(cbind(names, numbers))

I will get this:


   names numbers
1     A      33
2     B      44
3     C      55
4     D      66

But how do I assign the numbers (e.g. 33) to objects (e.g. A)

share|improve this question
What programming language are you writing in? Please tag the question accordingly. –  Matt Ball Feb 3 '12 at 1:13

2 Answers 2

It does not look like a very good idea: your function would be assigning variables in the global environment, or in its parent environment, instead of returning something. If you want to return several values, you can put them in a named list, e.g., list(A=3.14, B=2.71), or a vector if they all have the same type (they do, if you can put them in a data.frame).

In addition, in your example, cbind converts the numbers into factors: I am not sure this is intentional.

However, if you really insist, this can be done with assign.

d_ply( MYdataframe, "names", function(u) 
  assign( as.character(u$names[1]), u$numbers, envir=.GlobalEnv) 
share|improve this answer
+1 Caution is very strongly encouraged. R has no serious checking for whether or not the user is going to do something bad with an assignment. –  Iterator Feb 3 '12 at 3:39

If you really wanted to use the character values as names and the numeric values as "names' for a numeric vector then this would do it:

names(numbers) <- names
# A  B  C  D 
#33 44 55 66 
#  A 
# 33 

Maybe you should say what you really want, as well as choosing names for your objects that are not function names (names is a function) will help us keep things sorted out in our heads.

share|improve this answer

Your Answer


