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

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

Iterate through list of DFs, grab their listname and apply a function?

问题

  1. 我有一个正确命名的数据帧列表。我想使用这些数据帧创建circos图并在for循环中保存它们。
  2. 以下是我创建图表的脚本:
  3. ``` R
  4. jpeg("df-name.jpeg")
  5. circos.initialize(df$sectors, x = df$x)
  6. circos.track(df$sectors, y = df$y,
  7. panel.fun = function(x, y) {
  8. circos.text(CELL_META$xcenter,
  9. CELL_META$cell.ylim[2] + mm_y(5),
  10. CELL_META$sector.index)
  11. circos.axis(labels.cex = 0.6)
  12. })
  13. dev.off()

我认为我需要像这样做,但它不起作用,因为它无法获取列表名称:

  1. for (df in dflist) {
  2. jpeg(paste0(df, ".jpeg"))
  3. circos.initialize(df$sectors, x = df$x)
  4. circos.track(df$sectors, y = df$y,
  5. panel.fun = function(x, y) {
  6. circos.text(CELL_META$xcenter,
  7. CELL_META$cell.ylim[2] + mm_y(5),
  8. CELL_META$sector.index)
  9. circos.axis(labels.cex = 0.6)
  10. })
  11. dev.off()
  12. }

虽然这能获取列表名称,但无法获取数据帧:

  1. dfnames <- names(dflist)
  2. for (df in seq_along(dflist)) {
  3. print(df.names[i])
  4. }

我该如何在R中同时获取列表名称和数据帧,并在for循环中使用它们?非常感谢答案。

我找到了答案,原来如此简单:

  1. for (name in names(dflist)) {
  2. df <- dflist[[name]]
  3. drawCircos(df, name)
  4. }

我将脚本包装在一个函数中,然后应用了上面的for循环。

  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. 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()

  1. 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()
}

  1. 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])
}

  1. 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.
  2. I found the answer, it was that simple:

for (name in names(dflist)) {
df <- dflist[[name]]
drawCircos(df, name)
}

  1. I wrap the script in a function and applied the for loop above.
  2. </details>
  3. # 答案1
  4. **得分**: 2
  5. 我们可以遍历`dflist`中的名称,子集内的数据来创建一个临时对象`df`并使用相同的代码
  6. ```R
  7. for (nm in names(dflist)) {
  8. jpeg(paste0(nm, ".jpeg"))
  9. df <- dflist[[nm]]
  10. circos.initialize(df$sectors, x = df$x)
  11. circos.track(df$sectors, y = df$y,
  12. panel.fun = function(x, y) {
  13. circos.text(CELL_META$xcenter,
  14. CELL_META$cell.ylim[2] + mm_y(5),
  15. CELL_META$sector.index)
  16. circos.axis(labels.cex = 0.6)
  17. })
  18. dev.off()
  19. }
英文:

We may loop over the names of the dflist, subset the data inside to create a temporary object df and use the same code

  1. for (nm in names(dflist)) {
  2. jpeg(paste0(nm, &quot;.jpeg&quot;))
  3. df &lt;- dflist[[nm]]
  4. circos.initialize(df$sectors, x = df$x)
  5. circos.track(df$sectors, y = df$y,
  6. panel.fun = function(x, y) {
  7. circos.text(CELL_META$xcenter,
  8. CELL_META$cell.ylim[2] + mm_y(5),
  9. CELL_META$sector.index)
  10. circos.axis(labels.cex = 0.6)
  11. })
  12. dev.off()
  13. }

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:

确定