1

My data frame is as follows:

structure(list(Keyword = c("Sales", "Info", "Reply", "Service", 
"Accounting", "donotreply"), Class = c("Check", "Send", "Do Not Send", 
"Check", NA, "Do Not Send")), class = "data.frame", row.names = c(NA, 
-6L))

I am trying to transform the Keywords that are "Do Not Send" into their own vector by piping them into an as.vector. The following is my code thus far:

DNSkeywords <- data.frame(read_excel('Keywords List.xlsx', sheet = 'Sheet1', col_names = TRUE)) %>% 
  filter(Class == 'Do Not Send') %>% 
  select(Keyword) %>% 
  as.vector(.$Keyword)

My output should simply be the following:

c("Reply", "donotreply")

Granted, I could write a second line of DNSkeywords <- as.vector(DNSkeywords$Keyword), but I would rather pipe it in. I'm unsure how to utilize the as.vector() command inside the tidyverse such that I can select a single column in the tibble.

Yehuda
  • 1,787
  • 2
  • 15
  • 49
  • Not sure what is the issue. If I use the dput output and with convenient function `pull` `df1 %>% filter(Class == 'Do Not Send') %>% pull(Keyword)#[1] "Reply" "donotreply"` – akrun Jan 02 '19 at 16:44
  • 1
    Perhaps Hadley Wickham wrote his book before this function was released with the dplyr package, because I didn't see it mentioned there.... I'm actually only noticing it now on the dplyr site. Boy do I feel silly. – Yehuda Jan 02 '19 at 16:56

1 Answers1

3

You can use pull or pluck as follows.

library(tidyverse)

dat %>%
  filter(Class == 'Do Not Send') %>% 
  pull(Keyword)

dat %>%
  filter(Class == 'Do Not Send') %>% 
  pluck("Keyword")

Or just [[ from base R.

dat %>%
  filter(Class == 'Do Not Send') %>% 
  `[[`("Keyword")

Or unlist if you really want to use select, but this one is wordy.

dat %>%
  filter(Class == 'Do Not Send') %>% 
  select(Keyword) %>%
  unlist(use.names = FALSE)

DATA

dat <- structure(list(Keyword = c("Sales", "Info", "Reply", "Service", 
                           "Accounting", "donotreply"), Class = c("Check", "Send", "Do Not Send", 
                                                                  "Check", NA, "Do Not Send")), class = "data.frame", row.names = c(NA, 
                                                                                                                                    -6L))
www
  • 38,575
  • 12
  • 48
  • 84