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.
d$Date,d$`Product 1`, etc. Or useattach(which I do not recommend).list2env(data, .GlobalEnv)might be what you want. It sends the columns to the global environment each as its own variable.indx <- gsub("[^0-9]+", "", colnames(dat)[-1]); lst1 <- lapply(split(seq_along(dat[,-1]), indx), function(i) colnames(dat[,-1][,i]))wheredatis your dataset. Then you could uselist2envas Richard Scriven suggested. i.e.list2env(setNames(lst1, paste("Product_Index", names(lst1), sep="_")), envir=.GlobalEnv); Product_Index_1