gpt4 book ai didi

regex - 使用 lapply 在列表中绘制数据并使用列表元素的名称作为绘图标题

转载 作者:行者123 更新时间:2023-12-04 17:47:02 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Adding lists names as plot titles in lapply call in R

(1 个回答)


6年前关闭。




如果我有以下列表:

comp.surv <- list(a = 1:4, b = c(1, 2, 4, 8), c = c(1, 3, 8, 27))
comp.surv
# $a
# [1] 1 2 3 4
#
# $b
# [1] 1 2 4 8
#
# $c
# [1] 1 3 8 27

我可以用 lapply绘制每个列表元素:
lapply(comp.surv, function(x) plot(x))

但是,我想将每个列表元素的名称作为绘图标题( main )。对于我的示例数据,每个图表的标题是 a , bc分别。首先,我有一个 gsub给出的规则 comp.surv$a , 我返回 a :
gsub(comp.surv\\$([a-z]+), "\\1", deparse(sustitute((comp.surv$a)))
# "a"

哪个好。但是我无法将此结果嵌入到我的 lapply 中上面的声明。有任何想法吗?

与此同时,我尝试通过创建一个包含主要参数的函数来解决这个问题:
splot <- function(x){
plot(x, main = gsub(comp.surv\\$([a-z]+), "\\1" deparse(sustitute((x))))
}

lapply(comp.surv, function(x) splot(x))

这将绘制 comp.surv 的每个子变量,但所有标题都是空白的。

任何人都可以推荐我是否走在正确的轨道上?

最佳答案

一种可能性是循环遍历 names名单:

lapply(names(comp.surv), function(x) plot(comp.surv[[x]], main = x))

或者稍微详细一点,循环遍历列表索引:
lapply(seq_along(comp.surv), function(x) plot(comp.surv[[x]], main = names(comp.surv)[x]))

关于regex - 使用 lapply 在列表中绘制数据并使用列表元素的名称作为绘图标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30801554/

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