I have the following dataframe:
df <- data.frame(a = 1, b = 2, c = 3) I want to convert it to three named vectors:
a <- 1 b <- 2 c <- 3 How can I do this programmatically? tidyverse solutions especially appreciated.
You could do something like this (though @MartinGal gives a better method). We can convert each column to a list, then flatten to just have a named vector, then can save to the global environment.
library(tidyverse) list2env(flatten(apply(df, 2, function(x) as.list(x))), envir = .GlobalEnv) df[,'a'] doesn't get me what I want, which is to achieve the specified outcomes programmatically (i.e., without typing a command for each column).
df)? (2) If your actual data.frame isdata.frame(a = 1, b = 2, c = 3)I don't think you really want to convert it into three vectors, since you basically have them already (df[["a"]]for example).df[["a"]]doesn't get me what I want, which is to achieve the specified outcomes programmatically (i.e., without typing a command for each column).aand can go withdf["a"]or in atidyverseway with a curly-curly or bang-bang construct.rnorm(10,0,a).rnorm(10,0,df[["a"]])should work