遍历 DataFrame 列表,获取它们的列名并应用一个函数?

huangapple go评论76阅读模式
英文:

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&#39;t work because it can&#39;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&#39;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, &quot;.jpeg&quot;))
  df &lt;- 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()
}

huangapple
  • 本文由 发表于 2023年2月18日 23:08:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定