Extract Column from data.frame as a Vector

2019-02-04 04:21发布

问题:

I'm new to R.

I have a a Data.frame with a column called "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")). Which is the most concise way of doing this?

回答1:

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.

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