Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use dplyr to get values of a column [duplicate]

Tags:

r

dplyr

I'd like to have dplyr return a character vector instead of a data frame. Is there an easy way to do this?

#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
like image 577
zach Avatar asked Mar 17 '23 07:03

zach


1 Answers

Try

library(dplyr)
df %>%
   select(z) %>%
   unique() %>% 
   .$z
#[1] "a" "d"

Or using magrittr

library(magrittr)
df %>%
  select(z) %>%
  unique() %>%
  use_series(z)
#[1] "a" "d"
like image 120
akrun Avatar answered Apr 01 '23 16:04

akrun