Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R friendly way to convert lots of R data frame columns to lots of vectors

I looked at this solution: R-friendly way to convert R data.frame column to a vector?

but each solution seems to involve manually declaring the name of the vector being created.

I have a large dataframe with about 224 column names. I would like to break up the data frame and turn it into 224 different vectors which preserve their label without typing them all manually. Is there a way to step through the columns in the data frame and produce a vector which has the same name as the column or am I dreaming?

like image 204
Jesse_m_m Avatar asked Jan 30 '26 21:01

Jesse_m_m


2 Answers

I think it's a bad idea but this would work (using mtcars data set):

list2env(mtcars, .GlobalEnv)
like image 197
Tyler Rinker Avatar answered Feb 02 '26 11:02

Tyler Rinker


attach is another dangerous command that people use to be able to access the columns of a data frame directly with their names. If you don't know why it's dangerous, though, don't do it.

like image 22
Aaron left Stack Overflow Avatar answered Feb 02 '26 09:02

Aaron left Stack Overflow