Now this is supported directly with bind_rows (introduced in dplyr 0.7.0 ):
library(tidyverse)) vec <- c("a" = 1, "b" = 2) bind_rows(vec) #> # A tibble: 1 x 2 #> ab #> <dbl> <dbl> #> 1 1 2
This quote from https://cran.r-project.org/web/packages/dplyr/news.html explains the change:
bind_rows() and bind_cols() now accept vectors. They are considered as rows of the first and columns of the last. Rows require internal names, such as c(col1 = 1, col2 = 2) , and columns require external names: col1 = c(1, 2) . Lists are still treated as data frames, but they can be explicitly joined using !!! , eg. bind_rows(!!! x) (# 1676).
With this change, this means the following line in the usage example:
txt %>% map(read_xml) %>% map(xml_attrs) %>% map_df(~t(.) %>% as_tibble)
can be rewritten as
txt %>% map(read_xml) %>% map(xml_attrs) %>% map_df(bind_rows)
which is also equivalent
txt %>% map(read_xml) %>% map(xml_attrs) %>% { bind_rows(!!! .) }
The equivalence of the various approaches is demonstrated in the following example:
library(tidyverse) library(rvest) txt <- c('<node a="1" b="2"></node>', '<node a="1" c="3"></node>') temp <- txt %>% map(read_xml) %>% map(xml_attrs) # x, y, and z are identical x <- temp %>% map_df(~t(.) %>% as_tibble) y <- temp %>% map_df(bind_rows) z <- bind_rows(!!! temp) identical(x, y) #> [1] TRUE identical(y, z) #> [1] TRUE z #> # A tibble: 2 x 3 #> abc #> <chr> <chr> <chr> #> 1 1 2 <NA> #> 2 1 <NA> 3
source share