我想让dplyr返回一个字符向量而不是数据帧。有一个简单的方法吗?
#example data frame
df <- data.frame( x=c('a','b','c','d','e','f','g','h'),
y=c('a','a','b','b','c','c','d','d'),
z=c('a','a','a','a','a','a','d','d'),
stringsAsFactors = FALSE)
#desired output
unique(df$z)
[1] "a" "d"
#dplys's output
df %>%
select(z) %>%
unique()
z
1 a
7 d
答案 0 :(得分:7)
尝试
library(dplyr)
df %>%
select(z) %>%
unique() %>%
.$z
#[1] "a" "d"
或使用magrittr
library(magrittr)
df %>%
select(z) %>%
unique() %>%
use_series(z)
#[1] "a" "d"