从数据框中提取列为Vector [英] Extract Column from data.frame as a Vector

查看:216
本文介绍了从数据框中提取列为Vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的新人。

我有一个名为Symbol的列的Data.frame。

I have a a Data.frame with a column called "Symbol".

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

我需要将其值作为向量存储( x = c(IDEA, PFC,RPL,SOBHA))。这是最简洁的方法?

I need to store its values as a vector(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建议使用以下代码。

VitoshKa suggested to use the following code.

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.

这两种方法如何不同:

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

cat(new.variable)
#IDEA PFC RPL SOBHA

这篇关于从数据框中提取列为Vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆