简体   繁体   English

as_tibble控制拼合级别

[英]as_tibble control level of flattening

In the example below I have a list where one element is a list with a sublist. 在下面的示例中,我有一个列表,其中一个元素是带有子列表的列表。 It appears that as_tibble() is flattening down until the 3rd level of list before stopping. 似乎在停止之前, as_tibble()平直到列表的第3级。 It would be nice to be able to control the flattening so I could preserve the entire list with sublist in element "c" of the example. 能够控制展平会很好,因此我可以在示例的元素"c"中保留带有子列表的整个列表。 I suspect some combination of map() , flatten() , etc. is needed, but cannot figure it out. 我怀疑需要map()flatten()等的某种组合,但无法弄清楚。

Issue Reproduced 转载的问题

as_tibble() returns 2 rows, but I really only want 1 row with a column corresponding to each list element ("a", "b", "c"). as_tibble()返回2行,但实际上我只希望1行具有​​与每个列表元素(“ a”,“ b”,“ c”)相对应的列。

library(dplyr)
x <- list(a = 1, 
          b = 2, 
          c = list(x=list(y=3, 
                          z=4), 
                   xx=NULL))
parsed <- as_tibble(x)
parsed
#> # A tibble: 2 x 3
#>       a     b c         
#>   <dbl> <dbl> <list>    
#> 1    1.    2. <list [2]>
#> 2    1.    2. <NULL>

parsed[1,]$c
#> $x
#> $x$y
#> [1] 3
#> $x$z
#> [1] 4

parsed[2,]$c
#> $xx
#> NULL

Desired Behavior where I unnest only the first level 我只嵌套第一级的期望行为

parsed <- as_tibble(x)
parsed
#> # A tibble: 1 x 3
#>       a     b c         
#>   <dbl> <dbl> <list>    
#> 1    1.    2. <list [2]>

parsed[1,]$c
#> $x
#> $x$y
#> [1] 3
#> $x$z
#> [1] 4
#> $xx
#> NULL

This is not elegant but here is one way to do it. 这不是很优雅,但这是实现它的一种方法。

library(purrr)
x <- list(a = 1, 
          b = 2, 
          c = list(x=list(y=3, 
                          z=4), 
                   xx=NULL))

# define a function to preserve the list
unnest_one_level <- function(x){
  if(is.list(x)) list(x) else x
}

res <- x %>% map_dfr(unnest_one_level)
res
#> # A tibble: 1 x 3
#>       a     b c         
#>   <dbl> <dbl> <list>    
#> 1    1.    2. <list [2]>

res$c
#> [[1]]
#> [[1]]$x
#> [[1]]$x$y
#> [1] 3
#> 
#> [[1]]$x$z
#> [1] 4
#> 
#> [[1]]$xx
#> NULL

as.tibble will repeat each column recycle each column value till they all have a shared number of elements. as.tibble将重复每一列,回收每个列的值,直到它们都具有共享数量的元素。 Here you are a and b have length 1, but c has length 2. So it seems you really want to treat it as a length one list, you just need to nest it in a list. 在这里, ab长度为1,而c长度为2。因此,您似乎真的想将其视为一个列表的长度,只需要将其嵌套在列表中即可。 I think this will do what you want in this case at least. 我认为这至少可以满足您的需求。

parsed <- as_tibble(modify_if(x, ~length(.x)>1, list))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 as_tibble()无法正常工作 - as_tibble() not working as expected 避免在 dplyr 中重复 as_tibble() - Avoid repeating as_tibble() in dplyr 我可以控制 as_tibble 或 as.data.frame 如何处理 R 中的一维数据吗? - Can I control how as_tibble or as.data.frame treat one-dimensional data in R? as_tibble只返回一个变量 - as_tibble only returns a single variable 为什么as_tibble()round浮动到最接近的整数? - Why does as_tibble() round floats to the nearest integer? 如何在 R 中将 as_tibble() 格式的时间序列数据转换为 as_tsibble() 格式? - How to transform an as_tibble() format time series data into an as_tsibble() format in R? 带有Apache箭头的Sparklyr R失败,意外终止:未找到对象&#39;as_tibble&#39; - Sparklyr R with apache arrow fails, terminated unexpectedly: object 'as_tibble' not found 为什么 as_tibble(mtcars,rownames = NA) 不显示行名? 文档说它应该 - Why does as_tibble(mtcars,rownames = NA) not show the row names? The documentation says that it should 这两个类有什么区别,为什么我必须在我的代码中使用 as_tibble function? - what's the difference between these two classes and why I must use as_tibble function in my code? 如何修复R中的“错误:&#39;名称空间:dplyr&#39;未导出对象&#39;as_tibble&#39;错误:包&#39;BLANK&#39;的延迟加载失败” - How to fix “Error : object ‘as_tibble’ is not exported by 'namespace:dplyr' ERROR: lazy loading failed for package ‘BLANK’” in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM