简体   繁体   English

从 data.frame 中提取列作为向量

[英]Extract Column from data.frame as a Vector

I'm new to R.我是 R 的新手。

I have aa Data.frame with a column called "Symbol".我有一个 Data.frame,其中有一列名为“Symbol”。

   Symbol
1   "IDEA"
2   "PFC"
3   "RPL"
4   "SOBHA"

I need to store its values as a vector( x = c("IDEA","PFC","RPL","SOBHA") ).我需要将其值存储为向量( x = c("IDEA","PFC","RPL","SOBHA") )。 Which is the most concise way of doing this?哪种方法最简洁?

your.data <- data.frame(Symbol = c("IDEA","PFC","RPL","SOBHA"))
new.variable <- as.vector(your.data$Symbol) # this will create a character vector

VitoshKa suggested to use the following code. VitoshKa 建议使用以下代码。

new.variable.v <- your.data$Symbol # this will retain the factor nature of the vector

What you want depends on what you need.你想要什么取决于你需要什么。 If you are using this vector for further analysis or plotting, retaining the factor nature of the vector is a sensible solution.如果您将此向量用于进一步分析或绘图,保留向量的因子性质是一个明智的解决方案。

How these two methods differ:这两种方法有何不同:

cat(new.variable.v)
#1 2 3 4

cat(new.variable)
#IDEA PFC RPL SOBHA

Roman Luštrik provided an excellent answer, however, the $ notation often proves hard to use in a pipe. Roman Luštrik 提供了一个很好的答案,但是, $表示法通常很难在管道中使用。 In a pipe, use the dplyr function pull() .在管道中,使用dplyr函数pull()

# setting up
library(tidyverse)
# import tidyverse for dplyr, tibble, and pipe
   
df <- data.frame(Symbol = c("IDEA","PFC","RPL","SOBHA"))
> df
  Symbol
1   IDEA
2    PFC
3    RPL
4  SOBHA

Now that the data frame is set up, we will first do some random mutates to the data frame just to show that it will work in a pipe, and at the end, we will use pull() .现在数据框已经设置好了,我们将首先对数据框进行一些随机变异,以表明它可以在管道中工作,最后,我们将使用pull()

myvector <- df %>%
  mutate(example_column_1 = 1:4, example_column_2 = example_column_1^2) %>% #random example function
  arrange(example_column_1) %>% #random example function
  pull(Symbol) # finally, the pull() function; make sure to give just the column name as an argument

You can even further manipulate the vector in the pipe after the pull() function.您甚至可以在pull()函数之后进一步操作管道中的向量。

> myvector
[1] IDEA  PFC   RPL   SOBHA
Levels: IDEA PFC RPL SOBHA
> typeof(myvector)
[1] "integer"

typeof(myvector) returns integer because that is how factors are stored, where the different levels of the factor are stored as integers (I'm think that is how they are stored, at least). typeof(myvector)返回整数,因为这就是因子的存储方式,因子的不同级别存储为整数(我认为它们至少是这样存储的)。 If you want to convert to character vector, just use as.character(myvector) .如果要转换为字符向量,只需使用as.character(myvector)

In conclusion, use dplyr 's pull() function (and input just the column name you want to extract) when you want to extract a vector from a data frame or tibble while in a pipe .总之,当您想在管道中从数据框或 tibble 中提取向量时,请使用dplyrpull()函数(并仅输入您要提取的列名)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM