Using pull() with c() in R to combine vectors

Viewed 41

I've just come across the pull() function and the output is somehow different when using it in a pipeline. See this example:

## What's the problem with pull()?
##
install.packages("hflights")
library(hflights)
library(magrittr)

## Here we create two vectors
##
origins <- pull(hflights, Origin)
destinations <- pull(hflights, Dest)

## Here we combine them into one vector and retrieve the unique values:
##

c(origins, destinations) %>% unique()

## So what's the problem with this?:
## It returns a nested list.

hflights %>% c(pull(.,Origin), pull(.,Dest)) %>% unique()
1 Answers

you will get your expected result by using curly braces around the complex middle section

hflights %>% {c(pull(.,Origin), pull(.,Dest)) }%>% unique()
Related