Create separate vectors for each of a data frame's columns (variables)

馋奶兔 提交于 2021-02-16 20:05:25

问题


Goal: Take a data frame and create separate vectors for each of its columns (variables).

The following code gets me close:

batting <- read.csv("mlb_2014.csv", header = TRUE, sep = ",")
hr <- batting[(batting$HR >= 20 & batting$PA >= 100), ]
var_names <- colnames(hr)
for(i in var_names) {
path <- paste("hr$", i, sep = "")
assign(i, as.vector(path))
}

It creates the a vector for each column in the data frame as shown by the output below:

> ls()
 [1] "AB"          "Age"         "BA"          "batting"     "BB"          "CS"         
 [7] "G"           "GDP"         "H"           "HBP"         "hr"          "HR"         
 [13] "i"           "IBB"         "Lg"          "Name"        "OBP"         "OPS"        
 [19] "OPS."        "PA"          "path"        "Pos.Summary" "R"           "RBI"        
 [25] "SB"          "SF"          "SH"          "SLG"         "SO"          "TB"         
 [31] "Tm"          "var_names"   "X2B"         "X3B"        

So far so good until you call one of the vectors. For example:

AB
[1] "hr$AB"

Alas, all that is created is a one element character vector. When what I want it to create is this...

> AB <- as.vector(hr$AB)
> AB
[1] 459 456 506 417 492 496 404 430 497 346 494 501 415 370 500 331 501 539 456 443 316 437
[23] 449 526 349 486 432 480 295 489 354 506 315 471

...for each variable in the original data frame.

How do I get R to recognize the elements in the character vector "path" as objects to call in the assign function, rather than an individual character element to assign to the vector I'm creating? I would like to keep this within the loop frame work, since the main motivation behind this project is teach my self how to use loops.

Thanks!


回答1:


We have list2env for this:

list2env(iris, .GlobalEnv)
head(Species)
#[1] setosa setosa setosa setosa setosa setosa
#Levels: setosa versicolor virginica

However, there is almost never a reason to pollute your workspace like that.

Edit:

Here is how you can do this with a loop:

var_names <- colnames(iris)
for(i in var_names) {
  assign(i, iris[[i]])
}

Note that instead of creating your paths I use [[ to access the data.frame columns. If you have a column name as a character vector, that (or [) is the way to use this character to access the column.




回答2:


As @Roland mentions, you generally don't want to do that. Life is easier in the long run if you keep things together in lists, environments, or data frames.

A better approach is to learn how to use the with, within and related functions. These will temporarily attach a list, environment, or data frame to the beginning of the search path so that you can refer to the elements/columns directly by name:

> with(iris, head( Sepal.Width/Petal.Length ) )
[1] 2.500000 2.142857 2.461538 2.066667 2.571429 2.294118

These functions give you the convenience without polluting the global environment or search path.



来源:https://stackoverflow.com/questions/25750542/create-separate-vectors-for-each-of-a-data-frames-columns-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!