One of the following should do it for you, assuming your data.frame
is named "mydf".
lapply(names(mydf), function(x) assign(x, mydf[x], envir = .GlobalEnv))
lapply(names(mydf), function(x) assign(x, mydf[, x], envir = .GlobalEnv))
The first will create single-column data.frame
s, and the second will create vector
s.
Example in a clean session:
> rm(list = ls())
> ls()
character(0)
> mydf <- data.frame(A = c(1, 2), B = c(3, 4))
> mydf
A B
1 1 3
2 2 4
> invisible(lapply(names(mydf), function(x) assign(x, mydf[x], envir = .GlobalEnv)))
> ls()
[1] "A" "B" "mydf"
> A
A
1 1
2 2
> rm(list = ls())
> mydf <- data.frame(A = c(1, 2), B = c(3, 4))
> invisible(lapply(names(mydf), function(x) assign(x, mydf[, x], envir = .GlobalEnv)))
> ls()
[1] "A" "B" "mydf"
> B
[1] 3 4
In the examples above in invisible
to suppress the output.