How can we extract a named vector whose names come from one data.frame column and values from another, conveniently, using the pipe? (and without assigning in between)
Here is a very manual approach
vec <- iris %>%
arrange(Sepal.Length) %>%
pull(Sepal.Length)
names_for_vec <- iris %>%
arrange(Sepal.Length) %>%
pull(Species)
names(vec) <- names_for_vec
names(vec)
# Named vector
vec
Ideally, I'd like to achieve the same result in one line
What I've tried
I've tried variations on the top two answers here, as well as another idea of using "names<-"()
:
iris %>%
arrange(desc(Sepal.Length)) %>%
pull(Sepal.Length) %>%
`names<-`() # But we can't access the Species column as it was 2 pipes back..