提取 dplyr tbl 列作为向量

编程入门 行业动态 更新时间:2024-10-07 08:20:09
本文介绍了提取 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 列作为向量

本文发布于:2023-10-23 02:36:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519491.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   dplyr   tbl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!