Extract a dplyr tbl column as a vector

后端 未结 7 1280

Is there a more succinct way to get one column of a dplyr tbl as a vector, from a tbl with database back-end (i.e. the data frame/table can\'t be subset directly)?



        
相关标签:
7条回答
  • 2020-11-22 16:45

    I would use the extract2 convenience function from magrittr:

    library(magrittr)
    library(dplyr)
    
    iris2 %>%
      select(Species) %>%
      extract2(1)  
    
    0 讨论(0)
  • 2020-11-22 16:47

    @Luke1018 proposed this solution in one of the comments:

    You can also use the magrittr exposition operator (%$%) to pull a vector from a data frame.

    For example:

    iris2 %>% select(Species) %>% collect() %$% Species
    

    I thought it deserved its own answer.

    0 讨论(0)
  • 2020-11-22 16:48

    I'd probably write:

    collect(select(iris2, Species))[[1]]
    

    Since dplyr is designed for working with tbls of data, there's no better way to get a single column of data.

    0 讨论(0)
  • 2020-11-22 16:53

    With dplyr 0.7.0, you can use pull to get a vector from a tbl.


    library("dplyr")
    #> 
    #> Attaching package: 'dplyr'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     filter, lag
    #> The following objects are masked from 'package:base':
    #> 
    #>     intersect, setdiff, setequal, union
    db <- src_sqlite(tempfile(), create = TRUE)
    iris2 <- copy_to(db, iris)
    vec <- pull(iris2, Species)
    head(vec)
    #> [1] "setosa" "setosa" "setosa" "setosa" "setosa" "setosa"
    
    0 讨论(0)
  • 2020-11-22 16:56

    As per the comment from @nacnudus, it looks like a pull function was implemented in dplyr 0.6:

    iris2 %>% pull(Species)
    

    For older versions of dplyr, here's a neat function to make pulling out a column a bit nicer (easier to type, and easier to read):

    pull <- function(x,y) {x[,if(is.name(substitute(y))) deparse(substitute(y)) else y, drop = FALSE][[1]]}
    

    This lets you do either of these:

    iris2 %>% pull('Species')
    iris2 %>% pull(Species)
    iris2 %>% pull(5)
    

    Resulting in...

     [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4
    

    And it also works fine with data frames:

    > mtcars %>% pull(5)
     [1] 3.90 3.90 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 3.92 3.07 3.07 3.07 2.93 3.00 3.23 4.08 4.93 4.22 3.70 2.76 3.15 3.73 3.08 4.08 4.43
    [28] 3.77 4.22 3.62 3.54 4.11
    

    A nice way to do this in v0.2 of dplyr:

    iris2 %>% select(Species) %>% collect %>% .[[5]]
    

    Or if you prefer:

    iris2 %>% select(Species) %>% collect %>% .[["Species"]]
    

    Or if your table isn't too big, simply...

    iris2 %>% collect %>% .[["Species"]]
    
    0 讨论(0)
  • 2020-11-22 16:57

    You can also use unlist which I find easier to read because you do not need to repeat the name of the column or specify the index.

    iris2 %>% select(Species) %>% unlist(use.names = FALSE)
    
    0 讨论(0)
提交回复
热议问题