I have one data frame that contains columns that I want to look at individually. I am not sure what the common method is for analyzing data individually like this, but I want to create a separate variable/data frame for each column in my original data frame. I know I can subset, but is there a way I can use a for loop (is this the easiest way?) in order to create x new variables from the x columns in my data frame?
For more details on my data frame, I have a product and a corresponding index (which the product is being judged against).
Example data frame:
Date Product 1 Index 1 Product 2 Index 2
1/1/1995 2.89 2.75 4.91 5.01
2/1/1995 1.38 1.65 3.47 3.29
So I would like to create a variable for each product and corresponding index, without manually creating a data frame for each one, or subsetting when i want to analyze the product.
You could index the columns and put them into a new list with each element containing the product/index pair and the date column.
If you want, you can then assign these data frames to the global environment with
list2env
Data:
This is what
attach
does. You can just doattach(my_data_frame)
.Most people who know what they're doing would tell you this lies somewhere between "unnecessary" and "not a good idea".
Like someone mentioned in the comments, you can do this by indexing. But if you really want separate vectors for each column in your data frame, you could do it like this: