从data.frame中提取列作为Vector

st0*_*0le 25 r vector dataframe

我是R.的新手

我有一个Data.frame,其中一列名为"Symbol".

   Symbol
1   "IDEA"
2   "PFC"
3   "RPL"
4   "SOBHA"
Run Code Online (Sandbox Code Playgroud)

我需要将其值存储为vector(x = c("IDEA","PFC","RPL","SOBHA")).这样做最简洁的方法是什么?

Rom*_*rik 31

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

VitoshKa建议使用以下代码.

new.variable.v <- your.data$Symbol # this will retain the factor nature of the vector
Run Code Online (Sandbox Code Playgroud)

你想要什么取决于你需要什么.如果您使用此向量进行进一步分析或绘图,保留向量的因子性质是一个明智的解决方案.

这两种方法有何不同:

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

cat(new.variable)
#IDEA PFC RPL SOBHA
Run Code Online (Sandbox Code Playgroud)

  • 无需转换,your.data $符号将起作用. (6认同)