-2

I have a data frame in R but want all of the variables in the data frame to be single variables in my working space instead. So, I am looking for a command where I just use command(df) in the code below and I have Var_A, Var_B, Var_C in my working space.

data <- 1:12
df <- data.frame(matrix(data, ncol = 3))
names(df) <- c("Var_A", "Var_B", "Var_C")
df

> df
  Var_A Var_B Var_C
1     1     5     9
2     2     6    10
3     3     7    11
4     4     8    12

EDIT: My question is not an exact duplicate of the suggested question. The suggested question asks why it is not good to do what I want. There is a difference in asking how to do something and why doing something might be bad. Moreover, I don't understand the downvotes. I stated a clear question with a reproucible code sample. Instead of voting me down because one thinks it is a bad thing what I want to do, one could simply answer and propose an alternative.

stats_guy
  • 695
  • 1
  • 9
  • 26
  • 1
    Why do you need to create all the objects in the global environment? It is not so good – akrun Apr 07 '16 at 11:24
  • 3
    attach()ing a data.frame is probably never a good idea. It is a huge cause of bugs, and there are many better alternatives, for example with() or the data argument in functions that take a formula – Richard Telford Apr 07 '16 at 11:40
  • I don't understand the downvotes. I stated a clear question with a reproucible code sample. Instead of voting me down because one thinks it is a bad thing what I want to do, one could simply answer and propose an alternative. – stats_guy May 23 '16 at 17:28

2 Answers2

-1

Hi if you want to do that you can do :

list2env(x = df, envir = .GlobalEnv)
Var_A
# [1] 1 2 3 4
ls()
# [1] "data"  "df"    "Var_A" "Var_B" "Var_C"

EDIT you should probably keep your variables in your data.frame, or at least in a list, there's probably an other way to do what you want to do with data.frame.

Victorp
  • 13,636
  • 2
  • 51
  • 55
  • Brilliant!! I came up with an solution that split to single dataframes.. little non sense.. anyway for sake of discussion... `for(i in 1:ncol(df)){ assign(paste(names(df)[i]), df[i]) }` – G. Cocca Apr 07 '16 at 12:41
  • See this comment from the author of `list2env` himself http://stackoverflow.com/questions/25761656/assign-data-frame-name-to-list-elements-using-name-vector/25761850#comment40331663_25761850 – David Arenburg Apr 07 '16 at 12:43
  • I agree, I can't find a case where this is the right thing to do, but i do not know what the OP wants to do with that – Victorp Apr 07 '16 at 13:05
  • 1
    I agree with both of you. Not a good practice, but sometimes happens you need this approach.. in 1% of the cases according to the author of `list2env` :). I have been in that 1% too – G. Cocca Apr 07 '16 at 13:31
  • I never used this before and afterwards. I needed it for one thing. – stats_guy May 23 '16 at 17:27
-2

Not sure why you would want to do that, but here it goes:

decomposeFrame <- function(df){
  vars <- colnames(df)

  sapply(vars, function(x){
    eval(parse(text = paste0(x, "<<- ", "df$", x)))
    })
}
eminik
  • 145
  • 7