英文:
How to add relative number of list as title with lapply
问题
我有这个 reprex 以便让你了解我想要做什么
mtcar = mtcars[-11]
examples = list(mtcars, mtcar)
lapply(examples, function (x){
plot(density(x$mpg), main =
paste0('Graph for ', ' (', ')'))
})
我希望每个图都出现在括号中,括号中显示图所代表的列表的相关编号。因此,第一个列表中的图应该在括号中显示编号 1,第二个列表中的图应该显示编号 2。
我应该如何适当地修改这段代码呢?
谢谢!
英文:
I have this as reprex to let you understand what I am trying to do
mtcar = mtcars[-11]
examples = list(mtcars, mtcar)
lapply(examples, function (x){
plot(density(x$mpg), main =
paste0('Graph for ', ' (', ')'))
})
I would like each graph would appear in brackets with the related number of the list that graph is represented from. Therefore, the graph from the first list would have in the brackets number 1 and the second one, number 2.
How could I possibly properly modify the code ?
Thanks
答案1
得分: 3
使用Map
而不是lapply
:
Map(
function (x, i) {
plot(density(x$mpg), main = paste0('图表 for (', i, ')'))
},
examples, seq_along(examples)
)
英文:
Use Map
instead of lapply
:
Map(
function (x, i) {
plot(density(x$mpg), main = paste0('Graph for (', i, ')'))
},
examples, seq_along(examples)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论