简体   繁体   English

在 tibble 中,通过改变新的列表列从命名列表中提取名称

[英]In a tibble, extract names from named lists by mutating a new list-column

I have a tibble with list-columns.我有一个带有列表列的小标题。 One of those (let's call it info column) contains named lists.其中之一(我们称之为info列)包含命名列表。 For each row of the tibble, I want to mutate a new list-column that will contain a vector (nested).对于 tibble 的每一行,我想改变一个新的列表列,它将包含一个向量(嵌套)。 The elements of that vector will correspond to the names of a named list in the adjacent " info " list-column.该向量的元素将对应于相邻“ info ”列表列中命名列表的名称

Example例子

my_tibble <-
  structure(
  list(
    var_name = c("artworks", "sports","independence", "gender"),
    info = list(
      list(
        `Vincent van Gogh` = "The Starry Night",
        `Leonardo da Vinci` = "Mona Lisa",
        `Johannes Vermeer` = "Girl with a Pearl Earring",
        `Sandro Botticelli` = "The Birth of Venus",
        `Grant Wood` = "American Gothic"
      ),
      NULL,
      list(
        `1776` = "USA",
        `1818` = "Argentina",
        `1956` = "Morocco"
      ),
      list(male = "XY chromosomes",
           female = "XX chromosomes")
    )
  ),
  row.names = c(NA, -4L),
  class = c("tbl_df", "tbl", "data.frame")
)

> my_tibble
## # A tibble: 4 x 2
##   var_name     info            
##   <chr>        <list>          
## 1 artworks     <named list [5]>
## 2 sports       <NULL>          
## 3 independence <named list [3]>
## 4 gender       <named list [2]>

Desired Output所需 Output

var_name       info               names_of        
<chr>          <list>             <list>           
1 artworks     <named list [5]>   <chr [5]>     # c("Vincent van Gogh", "Leonardo da Vinci", "Johannes Vermeer", "Sandro Botticelli", "Grant Wood")
2 sports       <NULL>             <chr [1]>     # c("seems_null") 
3 independence <named list [3]>   <dbl [3]>     # c(1776, 1818, 1956)       
4 gender       <named list [2]>   <chr [2]>     # c("male", "female")

My attempt我的尝试

I want to mutate a new list-column, that checks the info column.我想改变一个新的列表列,它检查info列。 If info isn't NULL , the new list-column will contain a vector with the names of the list nested in info ;如果info不是NULL ,则新的 list-column 将包含一个向量,其中包含嵌套在info中的列表名称 otherwise, put a string "seems_null" in the mutated list-column.否则,将字符串"seems_null"放在变异的列表列中。

library(dplyr)
library(purrr)

my_tibble %>%
  mutate(names_of = map_chr(info, ~ ifelse(is.null(.x), "seems_null", names(.x))))

## # A tibble: 4 x 3
##   var_name     info             names_of        
##   <chr>        <list>           <chr>           
## 1 artworks     <named list [5]> Vincent van Gogh
## 2 sports       <NULL>           seems_null      
## 3 independence <named list [3]> 1776            
## 4 gender       <named list [2]> male  

Unfortunately, this returns only the first name from the list in info .不幸的是,这仅返回info列表中的名字。

I also tried using pmap() as shown in this answer , but it didn't work:我也尝试使用pmap()this answer所示,但它没有用:

my_list %>%
  mutate(names_of = map_chr(info, ~ ifelse(is.null(.x), list("seems_null"), pmap(.x, names(.x)[c(...)])  ) ))

Error: Problem with mutate() input names_of .错误: mutate()输入names_of有问题。
x invalid subscript type 'list' x 无效的下标类型“列表”
i Input names_of is map_chr(...) . i 输入names_ofmap_chr(...)

I'll appreciate any help!我将不胜感激任何帮助!

Perform the calculation rowwise like this:像这样按行执行计算:

res <- my_tibble %>%
   rowwise %>%
   mutate(names_of = list(if (is.null(info)) "seems_null" else names(info))) %>%
   ungroup

giving:给予:

> res
# A tibble: 4 x 3
  var_name     info             names_of 
  <chr>        <list>           <list>   
1 artworks     <named list [5]> <chr [5]>
2 sports       <NULL>           <chr [1]>
3 independence <named list [3]> <chr [3]>
4 gender       <named list [2]> <chr [2]>

暂无
暂无

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

相关问题 如何使用 tibble::tribble() 创建列表列? - How to create a list-column with tibble::tribble()? 将列表列添加到每个元素都是另一个列表列的第一个元素的 tibble - Adding a list-column to a tibble in which each element is the first element of another list-column 将 tibbles 的列表列添加到基于过滤器 quosures 的列表列的 tibble function - Add list-column of tibbles to a tibble based on a list-column of filter quosures without helper function 如何从具有不等维的列表列表的列表列中提取单个元素? - How to extract single element from a tibble with a list column which is a list-of-lists with unequal dimensions? 在 R 中,如何在不依赖任何先前列的小标题中创建一个新的列表列变量? - In R, how can I create a new list-column variable in a tibble that doesn't depend on any prior columns? 基于命名向量替换列表列中的值 - replace values in list-column based on named vector 为什么 tibble 会将 &quot;[, 1]&quot; 添加到从矩阵创建的新列名中? - Why does tibble add "[, 1]" to new column names created from a matrix? 如何在嵌套列表列中的 tibble 的字符列和行之间粘贴字符串 - How to paste strings between tibble's character column and rows in a nested list-column 从 tibble 创建列表列表 - Create list of lists from tibble 通过purrr和dplyr按组对小标题列表列的每个元素进行均值 - Mean across each element of a tibble list-column by group with purrr and dplyr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM