Extract a single dplyr tbl_df row as a vector
Using the dplyr
%>%
operator
library(dplyr)
as_tibble(mtcars) %>%
slice(2) %>%
unlist(., use.names=FALSE)
Or we can use c
with recursive=TRUE
as_tibble(mtcars) %>%
slice(2) %>%
c(., recursive=TRUE) %>%
unname
From Introduction to dplyr: "All of the dplyr functions take a data frame (or tibble) as the first argument." So no need to convert mtcars
into a tibble. Furthermore, as.numeric()
is more concise than unlist(., use.names = FALSE)
.
library(dplyr)
mtcars %>%
slice(2) %>%
as.numeric()