Extract a dplyr tbl column as a vector

后端 未结 7 1367

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

提交回复
热议问题