简体   繁体   English

如何将嵌套列表名称转换为带有行名称的 tibble

[英]How to convert nested list name into tibble with row names

I have the following nested list:我有以下嵌套列表:

l <- list(`1` = c(Fn1 = 85.3820037423659, Sema3c = 0.0362596376899117, 
App = 8.08423415658745), `3` = c(Fn1 = 11.0051859200698, Sema3c = 0, 
App = 44.0000000000306), `4` = c(Fn1 = 3.4594316186373, Sema3c = 0, 
App = 6.54280428377643))

That looks like this:看起来像这样:

$`1`
        Fn1      Sema3c         App 
85.38200374  0.03625964  8.08423416 

$`3`
     Fn1   Sema3c      App 
11.00519  0.00000 44.00000 

$`4`
     Fn1   Sema3c      App 
3.459432 0.000000 6.542804 

What I want to convert them into tibble.我想将它们转换为 tibble。

               `1`   `3`   `4`

  Fn1      85.4     11.0  3.46
  Semac3    0.0363   0    0   
  App      8.08    44.   6.54

How can I achieve that?我怎样才能做到这一点?

I tried tibble::as_tibble(l) but it doesn't show the row name.我试过tibble::as_tibble(l)但它没有显示行名。

Here is one way with tidyverse .这是tidyverse一种方式。 Since we have a numeric vector in the list we first convert it into a tibble, combine the list of tibbles into one dataframe using bind_rows , get the data in long format, bring it back to wide format changing the column names and turn the name column to rownames.由于列表中有一个数字向量,我们首先将其转换为bind_rows ,使用bind_rows将 tibbles 列表合并为一个数据帧,以长格式获取数据,将其恢复为宽格式,更改列名并转换name列到行名。

library(tidyverse)

map(l, ~as_tibble(t(.x))) %>%
    bind_rows(.id = 'id') %>%
    pivot_longer(cols = -id) %>%
    pivot_wider(names_from = id, values_from = value) %>%
    column_to_rownames('name')

#             1  3    4
#Fn1    85.3820 11 3.46
#Sema3c  0.0363  0 0.00
#App     8.0842 44 6.54

We can use unnest_wider我们可以使用unnest_wider

library(tidyr)
library(purrr)
tibble(col1 = map(l, as.list)) %>% 
       unnest_wider(col1) %>% 
       t

Or using transpose from purrr and map或者使用来自purrrmap transpose

library(tibble)
transpose(l) %>% 
    map_df(~ .x, .id = 'grp') %>%
    column_to_rownames('grp')
#               1        3        4
#Fn1    85.38200374 11.00519 3.459432
#Sema3c  0.03625964  0.00000 0.000000
#App     8.08423416 44.00000 6.542804

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

相关问题 如何将嵌套在 tibble 中的列表转换为单个 tibble,每个列表项都位于新行上? - How to convert lists nested in a tibble into a single tibble with each list item on a new row? 如果一行包含列表()/没有嵌套的tibble,如何过滤嵌套的tibble - How to filter nested tibble if one row contains list()/no nested tibble 将带有嵌套列表的列表转换为带有嵌套数据框的单行小标题 - Convert list with nested lists to one-row tibble with nested dataframe 如何在 tibble 的每一行和数据框的嵌套列表上应用 function - How to apply a function on each row of a tibble and a nested list of data frames 如何过滤掉带有嵌套列表的行而不是带有小标题的行? - How do I filter out the row with the nested list but not those with the tibble? 将部分嵌套的列表转换为嵌套的 tibble - Convert a partially nested list to a nested tibble 将嵌套列表转换为单个数据框或小标题 - Convert Nested list into single data frame or tibble 将列表列表转换为 data.frame 或 tibble (R) 的单个嵌套行 - Convert list of lists into single nested row of a data.frame or tibble (R) 将列表转换为Tibble Plus添加带有列表名称的列 - Convert List to Tibble Plus Add Column With List Names 如何将列表列表转换为小标题(数据框) - How to convert list of list into a tibble (dataframe)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM