tidyverse - 将命名向量转换为 data.frame/tibble 的首选方法 [英] tidyverse - prefered way to turn a named vector into a data.frame/tibble

查看:7
本文介绍了tidyverse - 将命名向量转换为 data.frame/tibble 的首选方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经常使用 tidyverse 我经常面临将命名向量转换为 data.frame/tibble 的挑战,其中列是向量的名称.
这样做的首选/tidyversey 方式是什么?
这与:thisthis github-issue

Using the tidyverse a lot i often face the challenge of turning named vectors into a data.frame/tibble with the columns being the names of the vector.
What is the prefered/tidyversey way of doing this?
This is related to: this and this github-issue

所以我想要:

require(tidyverse)
vec <- c("a" = 1, "b" = 2)

变成这样:

# A tibble: 1 × 2
      a     b
  <dbl> <dbl>
1     1     2

我可以通过例如:

vec %>% enframe %>% spread(name, value)
vec %>% t %>% as_tibble

用例示例:

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)

哪个给了

# A tibble: 2 × 3
      a     b     c
  <chr> <chr> <chr>
1     1     2  <NA>
2     1  <NA>     3

推荐答案

现在使用 bind_rows 直接支持(在 dplyr 0.7.0 中引入):

This is now directly supported using bind_rows (introduced in dplyr 0.7.0):

library(tidyverse)) 
vec <- c("a" = 1, "b" = 2)

bind_rows(vec)
#> # A tibble: 1 x 2
#>       a     b
#>   <dbl> <dbl>
#> 1     1     2

引自 https://cran.r-project.org/web/packages/dplyr/news.html 解释了变化:

bind_rows()bind_cols() 现在接受向量.前者将它们视为行,后者将它们视为列.行需要像 c(col1 = 1, col2 = 2) 这样的内部名称,而列需要外部名称:col1 = c(1, 2).列表仍被视为数据帧,但可以使用 !!! 显式拼接,例如bind_rows(!!! x) (#1676).

bind_rows() and bind_cols() now accept vectors. They are treated as rows by the former and columns by the latter. Rows require inner names like c(col1 = 1, col2 = 2), while columns require outer names: col1 = c(1, 2). Lists are still treated as data frames but can be spliced explicitly with !!!, e.g. bind_rows(!!! x) (#1676).

有了这个变化,这意味着用例示例中的以下行:

With this change, it means that the following line in the use case example:

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

可以改写为

txt %>% map(read_xml) %>% map(xml_attrs) %>% map_df(bind_rows)

也等价于

txt %>% map(read_xml) %>% map(xml_attrs) %>% { bind_rows(!!! .) }

以下示例展示了不同方法的等效性:

The equivalence of the different 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
#>       a     b     c
#>   <chr> <chr> <chr>
#> 1     1     2  <NA>
#> 2     1  <NA>     3

这篇关于tidyverse - 将命名向量转换为 data.frame/tibble 的首选方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆