nac*_*dus 150 r lazy-evaluation collect dplyr
是否有更简洁的方法将dplyr tbl的一列作为向量,从具有数据库后端的tbl(即数据帧/表不能直接是子集)?
require(dplyr)
db <- src_sqlite(tempfile(), create = TRUE)
iris2 <- copy_to(db, iris)
iris2$Species
# NULL
Run Code Online (Sandbox Code Playgroud)
这太容易了,所以
collect(select(iris2, Species))[, 1]
# [1] "setosa" "setosa" "setosa" "setosa" etc.
Run Code Online (Sandbox Code Playgroud)
但它似乎有点笨拙.
Lor*_*ert 150
使用dplyr 0.7.0,您可以使用pull从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"
Run Code Online (Sandbox Code Playgroud)
Tom*_*ell 87
根据@nacnudus的评论,看起来pull函数是在dplyr 0.6中实现的:
iris2 %>% pull(Species)
Run Code Online (Sandbox Code Playgroud)
对于旧版本的dplyr,这里有一个简洁的功能,可以使列更好(更容易键入,更容易阅读):
pull <- function(x,y) {x[,if(is.name(substitute(y))) deparse(substitute(y)) else y, drop = FALSE][[1]]}
Run Code Online (Sandbox Code Playgroud)
这使您可以执行以下任一操作:
iris2 %>% pull('Species')
iris2 %>% pull(Species)
iris2 %>% pull(5)
Run Code Online (Sandbox Code Playgroud)
导致...
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4
Run Code Online (Sandbox Code Playgroud)
它也适用于数据框:
> mtcars %>% pull(5)
[1] 3.90 3.90 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 3.92 3.07 3.07 3.07 2.93 3.00 3.23 4.08 4.93 4.22 3.70 2.76 3.15 3.73 3.08 4.08 4.43
[28] 3.77 4.22 3.62 3.54 4.11
Run Code Online (Sandbox Code Playgroud)
在v0.2中执行此操作的好方法dplyr:
iris2 %>% select(Species) %>% collect %>% .[[5]]
Run Code Online (Sandbox Code Playgroud)
或者如果您愿意:
iris2 %>% select(Species) %>% collect %>% .[["Species"]]
Run Code Online (Sandbox Code Playgroud)
或者,如果你的桌子不是太大,简单......
iris2 %>% collect %>% .[["Species"]]
Run Code Online (Sandbox Code Playgroud)
Sta*_*erc 67
您还可以使用unlist我发现更容易阅读的内容,因为您不需要重复列的名称或指定索引.
iris2 %>% select(Species) %>% unlist(use.names = FALSE)
Run Code Online (Sandbox Code Playgroud)
had*_*ley 20
我可能会写:
collect(select(iris2, Species))[[1]]
Run Code Online (Sandbox Code Playgroud)
由于dplyr是为处理tbls数据而设计的,因此没有更好的方法来获取单列数据.
Hug*_*ugh 20
我会使用extract2便利功能magrittr:
library(magrittr)
library(dplyr)
iris2 %>%
select(Species) %>%
extract2(1)
Run Code Online (Sandbox Code Playgroud)
rrs*_*rrs 16
@ Luke1018在其中一条评论中提出了这个解决方案:
您还可以使用
magrittrexposition运算符(%$%)从数据框中提取向量.
例如:
iris2 %>% select(Species) %>% collect() %$% Species
Run Code Online (Sandbox Code Playgroud)
我认为它应该得到自己的答案.
如果您习惯于使用方括号进行索引,另一种选择是将通常的索引方法包装在对deframe()的调用中,例如:
library(tidyverse)
iris2 <- as_tibble(iris)
# using column name
deframe(iris2[, 'Sepal.Length'])
# [1] 5.1 4.9 4.7 4.6 5.0 5.4
# using column number
deframe(iris2[, 1])
# [1] 5.1 4.9 4.7 4.6 5.0 5.4
Run Code Online (Sandbox Code Playgroud)
That 和pull()都是获得 tibble 列的好方法。
| 归档时间: |
|
| 查看次数: |
61283 次 |
| 最近记录: |