英文:
Iterate through list of DFs, grab their listname and apply a function?
问题
我有一个正确命名的数据帧列表。我想使用这些数据帧创建circos图并在for循环中保存它们。
以下是我创建图表的脚本:
``` R
jpeg("df-name.jpeg")
circos.initialize(df$sectors, x = df$x)
circos.track(df$sectors, y = df$y,
panel.fun = function(x, y) {
circos.text(CELL_META$xcenter,
CELL_META$cell.ylim[2] + mm_y(5),
CELL_META$sector.index)
circos.axis(labels.cex = 0.6)
})
dev.off()
我认为我需要像这样做,但它不起作用,因为它无法获取列表名称:
for (df in dflist) {
jpeg(paste0(df, ".jpeg"))
circos.initialize(df$sectors, x = df$x)
circos.track(df$sectors, y = df$y,
panel.fun = function(x, y) {
circos.text(CELL_META$xcenter,
CELL_META$cell.ylim[2] + mm_y(5),
CELL_META$sector.index)
circos.axis(labels.cex = 0.6)
})
dev.off()
}
虽然这能获取列表名称,但无法获取数据帧:
dfnames <- names(dflist)
for (df in seq_along(dflist)) {
print(df.names[i])
}
我该如何在R中同时获取列表名称和数据帧,并在for循环中使用它们?非常感谢答案。
我找到了答案,原来如此简单:
for (name in names(dflist)) {
df <- dflist[[name]]
drawCircos(df, name)
}
我将脚本包装在一个函数中,然后应用了上面的for循环。
<details>
<summary>英文:</summary>
I have a list of data frames which are named correctly in the list. I want to create circos plots using those data frames and save them using a for loop.
Here is the script where I create the plot:
jpeg("df-name.jpeg")
circos.initialize(df$sectors, x = df$x)
circos.track(df$sectors, y = df$y,
panel.fun = function(x, y) {
circos.text(CELL_META$xcenter,
CELL_META$cell.ylim[2] + mm_y(5),
CELL_META$sector.index)
circos.axis(labels.cex = 0.6)
})
dev.off()
I think I need to do something like this but it doesn't work because it can't grab list names:
for (df in dflist) {
jpeg(paste0(df, ".jpeg"))
circos.initialize(df$sectors, x = df$x)
circos.track(df$sectors, y = df$y,
panel.fun = function(x, y) {
circos.text(CELL_META$xcenter,
CELL_META$cell.ylim[2] + mm_y(5),
CELL_META$sector.index)
circos.axis(labels.cex = 0.6)
})
dev.off()
}
Although this grabs list names, it can't grab data frames:
dfnames <- names(dflist)
for (df in seq_along(dflist)) {
print(df.names[i])
}
How can I both grab list names and data frames and use them in a for loop in R? I appreciate very much the answers.
I found the answer, it was that simple:
for (name in names(dflist)) {
df <- dflist[[name]]
drawCircos(df, name)
}
I wrap the script in a function and applied the for loop above.
</details>
# 答案1
**得分**: 2
我们可以遍历`dflist`中的名称,子集内的数据来创建一个临时对象`df`并使用相同的代码
```R
for (nm in names(dflist)) {
jpeg(paste0(nm, ".jpeg"))
df <- dflist[[nm]]
circos.initialize(df$sectors, x = df$x)
circos.track(df$sectors, y = df$y,
panel.fun = function(x, y) {
circos.text(CELL_META$xcenter,
CELL_META$cell.ylim[2] + mm_y(5),
CELL_META$sector.index)
circos.axis(labels.cex = 0.6)
})
dev.off()
}
英文:
We may loop over the names of the dflist
, subset the data inside to create a temporary object df
and use the same code
for (nm in names(dflist)) {
jpeg(paste0(nm, ".jpeg"))
df <- dflist[[nm]]
circos.initialize(df$sectors, x = df$x)
circos.track(df$sectors, y = df$y,
panel.fun = function(x, y) {
circos.text(CELL_META$xcenter,
CELL_META$cell.ylim[2] + mm_y(5),
CELL_META$sector.index)
circos.axis(labels.cex = 0.6)
})
dev.off()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论