[英]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
”列表列中命名列表的名称。
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]>
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")
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()
inputnames_of
.错误:mutate()
输入names_of
有问题。
x invalid subscript type 'list' x 无效的下标类型“列表”
i Inputnames_of
ismap_chr(...)
. i 输入names_of
是map_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.