0

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.

3
  • 1
    I don't see why you would want to do that. Either use indexing such as d$Date, d$`Product 1`, etc. Or use attach (which I do not recommend). Commented Oct 3, 2014 at 13:34
  • 2
    list2env(data, .GlobalEnv) might be what you want. It sends the columns to the global environment each as its own variable. Commented Oct 3, 2014 at 14:43
  • @user2662565 If you can store the variable name pairs in a list, you could do: indx <- gsub("[^0-9]+", "", colnames(dat)[-1]); lst1 <- lapply(split(seq_along(dat[,-1]), indx), function(i) colnames(dat[,-1][,i])) where dat is your dataset. Then you could use list2env as Richard Scriven suggested. i.e. list2env(setNames(lst1, paste("Product_Index", names(lst1), sep="_")), envir=.GlobalEnv); Product_Index_1 Commented Oct 3, 2014 at 15:25

3 Answers 3

1

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:

df <- data.frame(x=1:10, y=11:20, z=21:30)

for (i in colnames(df)) {
    assign(i, df[, i])
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could index the columns and put them into a new list with each element containing the product/index pair and the date column.

ind <- seq(2, by = 2, length.out = ncol(dat[-1])/2)
(sets <- lapply(ind, function(i) dat[c(1, i:(i+1))]))
# [[1]]
#       Date Product1 Index1
# 1 1/1/1995     2.89   2.75
# 2 2/1/1995     1.38   1.65
# 
# [[2]]
#       Date Product2 Index2
# 1 1/1/1995     4.91   5.01
# 2 2/1/1995     3.47   3.29

If you want, you can then assign these data frames to the global environment with list2env

list2env(setNames(sets, paste0("Set", seq_along(sets))), .GlobalEnv)
Set1
#       Date Product1 Index1
# 1 1/1/1995     2.89   2.75
# 2 2/1/1995     1.38   1.65
Set2
#       Date Product2 Index2
# 1 1/1/1995     4.91   5.01
# 2 2/1/1995     3.47   3.29

Data:

dat <- 
structure(list(Date = structure(1:2, .Label = c("1/1/1995", "2/1/1995"
), class = "factor"), Product1 = c(2.89, 1.38), Index1 = c(2.75, 
1.65), Product2 = c(4.91, 3.47), Index2 = c(5.01, 3.29)), .Names = c("Date", 
"Product1", "Index1", "Product2", "Index2"), class = "data.frame", row.names = c(NA, 
-2L))

Comments

0

This is what attach does. You can just do attach(my_data_frame).

Most people who know what they're doing would tell you this lies somewhere between "unnecessary" and "not a good idea".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.