Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turning named vectors into data.frame/tibble #255

Closed
rentrop opened this issue Oct 26, 2016 · 4 comments
Closed

Turning named vectors into data.frame/tibble #255

rentrop opened this issue Oct 26, 2016 · 4 comments

Comments

@rentrop
Copy link

rentrop commented Oct 26, 2016

FEATURE REQUEST (hope that purrr is the right place for this)

Following up on this SO question:

A shortcut for turning named vectors into data.frame/tibble would be great:
For me a common task is to turn named vectors into data.frames. bind_rows and map_df do not handle this nicely. So you can do one of the following workarounds:

require(tidyverse)
vec <- c("a" = 1, "b" = 2)
# Version one
vec %>% enframe %>% spread(name, value)
# Version two
vec %>% t %>% as_tibble

A usecase in a purrr setting: (this is why i put the feature-request as a purrr issue)

require(tidyverse)
require(rvest)
txt <- c('<node a="1" b="2"></node>',
         '<node a="1" c="3"></node>')

txt %>% map(read_xml) %>% map(xml_attrs) %>% map_df(~t(.) %>% as_tibble)

Which gives you:

# A tibble: 2 × 3
      a     b     c
  <chr> <chr> <chr>
1     1     2  <NA>
2     1  <NA>     3
@lionel-
Copy link
Member

lionel- commented Oct 26, 2016

I think this is tidyverse/dplyr#1676

@jennybc
Copy link
Member

jennybc commented Oct 29, 2016

This XML example comes up for me frequently 👍.

@rentrop
Copy link
Author

rentrop commented Nov 25, 2016

One more case where it woule be handy: here

@hadley
Copy link
Member

hadley commented Mar 3, 2017

Can you file this in tibble please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants