Is there an already existing method of assigning each of those data.frame
objects to separate variables?
list(cyl4, cyl6, cyl8) <- split(mtcars, mtcars$cyl)
Error in list(x, y, z) <- split(mtcars, mtcars$cyl) : could not find function "list<-
Is there an already existing method of assigning each of those data.frame
objects to separate variables?
list(cyl4, cyl6, cyl8) <- split(mtcars, mtcars$cyl)
Error in list(x, y, z) <- split(mtcars, mtcars$cyl) : could not find function "list<-
You can use the assign
to assign a value to variable. The variable names are strings.
In the code below I've looped though the elements of the list and assigned names from the vector names
.
l <- split(mtcars, mtcars$cyl)
names <- c('cyl4', 'cyl6', 'cyl8')
for (i in 1:length(names)) assign(names[i], l[[i]])
Or the list2env
function as pointed out by @Roland (and others). The attach
function does something similar.
l <- split(mtcars, mtcars$cyl)
names(l) <- c('cyl4', 'cyl6', 'cyl8')
list2env(l, envir = .GlobalEnv)
Or
attach(l)
Although, as others have pointed out. This might not be the best idea; it's easier to keep track of things inside lists. You can just access them as need be with $
.
l$cyl4