提取一个dplyr tbl列作为向量 [英] Extract a dplyr tbl column as a vector

查看:140
本文介绍了提取一个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.

但似乎有点笨拙。 / p>

But it seems a bit clumsy.

推荐答案

使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆