gpt4 book ai didi

r - 映射列表,获取项目名称和内容

转载 作者:行者123 更新时间:2023-12-04 15:10:54 27 4
gpt4 key购买 nike

my_dfs <- list(
mtcars = mtcars,
diamonds = diamonds
)
map(.x = my_dfs, ~ print(.x %>% str)) # OK

但是如果我还想访问正在打印的列表中的项目的名称,我该怎么做呢?

map(.x = my_dfs, ~ print(.id)) # found a reference to .id in the docs.
Error in print(.id) : object '.id' not found

更好的是,我希望同时调用 map,例如

map(.x = my_dfs, ~ print('About to print str of ' [somehow get list item name here] .x %>% str))

最佳答案

使用imap()。内容将由 .x 引用,名称由 .y 引用。

z <- list(x = "whats in x",
y = "whats in y")

imap(z, ~ paste(.y, "has contents:", .x))
# $x
# [1] "x has contents: whats in x"
#
# $y
# [1] "y has contents: whats in y"

关于r - 映射列表,获取项目名称和内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65218636/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com