Extract Column from data.frame as a Vector

前端 未结 2 1798
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 22:01

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\"

2条回答
  •  青春惊慌失措
    2020-12-29 22:45

    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
    

提交回复
热议问题