Create Variable from Each Column in Data Frame

2020-05-03 09:50发布

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条回答
家丑人穷心不美
2楼-- · 2020-05-03 10:06

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))
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-05-03 10:16

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

查看更多
三岁会撩人
4楼-- · 2020-05-03 10:25

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])
}
查看更多
登录 后发表回答