how to assign multiple values in R

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<-

solution 1: list2env()

lst <- split(mtcars, mtcars$cyl)
names(lst) <- paste0('dat', seq_along(lst))
list2env(lst, envir=.GlobalEnv)

solution 2: assign()

l <- split(mtcars, mtcars$cyl)   
names <- c('cyl4', 'cyl6', 'cyl8')
for (i in 1:length(names)) assign(names[i], l[[i]])

solution 3: attach()

attach() function can bound the values to the variables

solution 4:

X <- list()
values <- seq(1,9) X[c('a','b')] <- values[c(2,4)]

  

 

posted @ 2017-06-21 08:46  何帅  阅读(211)  评论(0)    收藏  举报