提取 dplyr tbl 列作为向量 [英] Extract a dplyr tbl column as a vector
本文介绍了提取 dplyr tbl 列作为向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有更简洁的方法可以从具有数据库后端的 tbl 中获取 dplyr tbl 的一列作为向量(即数据框/表不能直接作为子集)?
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)?
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
获取向量>.
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"
这篇关于提取 dplyr tbl 列作为向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文