使用 tidyverse “unnest"tibble 中的 data.frame 列 [英] Using tidyverse to "unnest" a data.frame column inside a tibble

查看:21
本文介绍了使用 tidyverse “unnest"tibble 中的 data.frame 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些从 www 调用返回的数据,jsonliteas_tibble 以某种方式转换为 data.frame 列.

I'm working with some data which is returned from a www call which jsonlite and as_tibble somehow convert into a data.frame column.

这个 result 数据有一个 Id 整数列和一个 ActionCode data.frame 列,带有两个内部列.这些在控制台中显示为:

This result data has an Id integer column and an ActionCode data.frame column with two internal columns. these show in the console as:

> result
# A tibble: 117 x 2
  Id    ActionCode$Code $Name 
  <int> <chr>           <chr>
  1     A1              First Code
  2     A2              Second Code
  3     A3              Third Code
  4     A4              Fourth Code
  ...

这可以用 str() 来检查:

> result %>% str()
tibble [117 x 2] (S3: tbl_df/tbl/data.frame)
 $ Id : int [1:117] 1 2 3 4 ...
 $ ActionCode:'data.frame': 117 obs. of  2 variables:
  ..$ Code: chr [1:117] "A1" "A2" "A3" "A4" ...
  ..$ Name: chr [1:117] "First Code" "Second Code" "Third Code" "Fourth Code" ...

我见过例如https://tibble.tidyverse.org/articles/types.html 那种data.frame 列是完全合法的,但我正在努力研究如何从整洁的 dplyr 管道访问此列中的数据 - 例如我不能select(ActionCode$Code)

I've seen from e.g. https://tibble.tidyverse.org/articles/types.html that this sort of data.frame column is perfectly legal, but I'm struggling to work out how to access the data in this column from tidy dplyr pipelines - e.g. I can't select(ActionCode$Code)

有没有办法在 dplyr 管道中处理这些列?或者有没有办法以某种方式展平这些列类似于 unnest 可以在 list 列上使用的方式(尽管我在这里意识到我不创建额外的行 - 我只是扁平化列层次结构).

Is there a way to work with these columns in dplyr pipelines? Or is there a way to somehow flatten these columns similar to how unnest can be used on list columns (although I realise here that I'm not creating extra rows - I'm just flattening the column hierarchy).

即我试图找到一个可以输出的函数 foo:

i.e. I'm trying to find a function foo which can output:

> result %>% foo() %>% str()
tibble [117 x 2] (S3: tbl_df/tbl/data.frame)
 $ Id : int [1:117] 1 2 3 4 ...
 $ Code: chr [1:117] "A1" "A2" "A3" "A4" ...
 $ Name: chr [1:117] "First Code" "Second Code" "Third Code" "Fourth Code" ...


我无法提供 www 调用作为示例,但作为一个工作示例,我认为我看到的数据类型类似于:


I can't provide the www call as a sample, but as a working example I think the sort of data I am presented with is something like:

sample_data <- tibble(
  Id = 1:10,
  ActionCode = tibble(
    Code = paste0("Id", 1:10),
    Name = paste0("Name ", 1:10),
  )
)

推荐答案

Reconverting to data.frame with do.call 使列变平

Reconverting to data.frame with do.call flattens out the columns

library(dplyr)
library(stringr)
do.call(data.frame, sample_data) %>% 
    rename_at(vars(starts_with('ActionCode')), ~ 
        str_remove(., 'ActionCode\\.')) %>% 
    as_tibble

-输出

# A tibble: 10 x 3
#      Id Code  Name   
#   <int> <chr> <chr>  
# 1     1 Id1   Name 1 
# 2     2 Id2   Name 2 
# 3     3 Id3   Name 3 
# 4     4 Id4   Name 4 
# 5     5 Id5   Name 5 
# 6     6 Id6   Name 6 
# 7     7 Id7   Name 7 
# 8     8 Id8   Name 8 
# 9     9 Id9   Name 9 
#10    10 Id10  Name 10

这篇关于使用 tidyverse “unnest"tibble 中的 data.frame 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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