1

Lets take the following DF

> df <- data.frame(var1=5,var2=6,var3=7)
> df
  var1 var2 var3
1    5    6    7
> 

i now want to automatically have three variables been "created" and added to the global environement with their respective values, like so:

var1 <- 5
var2 <- 6
var3 <- 7

How can i do it?

Thank you in advance. André

Andre Elrico
  • 10,956
  • 6
  • 50
  • 69
  • 1
    Global variables are very very risky idea, could you elaborate on what is your goal in this attempt – Silence Dogood Oct 18 '16 at 10:05
  • i have found a rather complex function on the web that can evaluate my survey. I think by doing the above i can use it to my needs without having to dig deep and understand it fully. Its more or less a test and if it works i saved alot of time. I know its a ugly thing to do. – Andre Elrico Oct 18 '16 at 10:15

1 Answers1

1

First of all, you should ask yourself if there is no other way than assigning global variables, see Why is using assign bad? for some discussions about that topic. If you really want to do it that way, you could do

for(i in 1:ncol(df)){
    assign(colnames(df)[i], df[,i])
}

However, I would encourage you to find a different solution than assigning variables.

Community
  • 1
  • 1
tobiasegli_te
  • 1,413
  • 1
  • 12
  • 18