Extract Column from data.frame as a Vector

前端 未结 2 1796
隐瞒了意图╮
隐瞒了意图╮ 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:40

    Roman Luštrik provided an excellent answer, however, the $ notation often proves hard to use in a pipe. In a pipe, use the dplyr function 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().

    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.

    > 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). If you want to convert to character vector, just use 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.

提交回复
热议问题