Extract a single line dplyr tbl_df as a vector

How to extract one line from tbl_df as a vector? A simple subset with brackets [] gives 1 line tbl_df :

 library(dplyr) dat <- as_data_frame(mtcars) dat[2, ] Source: local data frame [1 x 11] mpg cyl disp hp drat wt qsec vs am gear carb (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) 1 21 6 160 110 3.9 2.875 17.02 0 1 4 4 

A similar problem for Extract the dplyr tbl column as a vector , but with (I think) several different solutions.

+5
source share
1 answer

Using the dplyr %>% operator

 library(dplyr) tbl_df(mtcars) %>% slice(2) %>% unlist(., use.names=FALSE) 

Or we can use c with recursive=TRUE

 tbl_df(mtcars) %>% slice(2) %>% c(., recursive=TRUE) %>% unname 
+4
source

Source: https://habr.com/ru/post/1243808/


All Articles