有没有更简洁的方法从具有数据库后端的 tbl 中获取 dplyr tbl 的一列作为向量(即数据框/表不能直接作为子集)?
require(dplyr) db <- src_sqlite(tempfile(), create = TRUE) iris2 <- copy_to(db, iris) iris2$Species # NULL
那太容易了,所以
collect(select(iris2, Species))[, 1] # [1] "setosa" "setosa" "setosa" "setosa" etc.
但这似乎有点笨拙。
使用 dplyr >= 0.7.0,您可以使用pull()从tbl.
pull()
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"