【问题标题】:Flow duration curve - title with the name of a data frame in a list流持续时间曲线 - 带有列表中数据框名称的标题
【发布时间】:2021-01-16 01:36:26
【问题描述】:

我想为每个站点的每日流量创建流量持续时间曲线 (FDC)。每个站点的每日流量被保存为列表中的动物园对象。当我为每个站绘制 FDC 时,我想在标题中放入与扩展名相对应的列表中数据框的名称。

这是我列表的结构

> str(listDF_zoo)
List of 2
 $ 094985005:‘zoo’ series from 2007-10-31 to 2020-09-29
  Data: Named num [1:4718] 0 0 0 0 0 0 0 0 0 0.01 ...
  ..- attr(*, "names")= chr [1:4718] "1" "2" "3" "4" ...
  Index:  Date[1:4718], format: "2007-10-31" "2007-11-01" "2007-11-02" "2007-11-03" ...
 $ 09498501 :‘zoo’ series from 1995-10-01 to 2020-09-29
  Data: Named num [1:9131] 0.14 0.08 0.08 0.07 0.07 0.06 0.07 0.07 0.07 0.07 ...
  ..- attr(*, "names")= chr [1:9131] "1" "2" "3" "4" ...
  Index:  Date[1:9131], format: "1995-10-01" "1995-10-02" "1995-10-03" "1995-10-04" ... 

这是我正在使用的代码:

FDC <- lapply(listDF_zoo,function(x){fdc(x, main=names(x))})

main=names(x) 的部分不起作用,这是我想要更改的部分

【问题讨论】:

    标签: r list plot time


    【解决方案1】:

    您可以使用purrrimap 来访问数据和名称:

    FDC <- purrr::imap(listDF_zoo, ~fdc(.x, main=.y))
    

    【讨论】:

    • 很好的发现 - 希望这样的东西能进入imap 文档...
    【解决方案2】:

    lapply,不幸的是,无法访问它正在使用的列表的名称。一种常见的解决方法是改用索引,例如,

    lapply(seq_along(listDF_zoo),
           function(i) fdc(listDF_zoo[[i]], main = names(listDF_zoo)[i])
    )
    

    【讨论】:

    • 或遍历名称:lapply(names(listDF_zoo), function(nm) fdc(listDF_zoo[[nm]], main = nm) 或使用 mapply(function(x, nm) fdc(x, main = nm), listDF_zoo, names(listDF_zoo))
    【解决方案3】:

    Map 的选项来自base R

    Map(fdc, listDF_zoo, main = names(listDF_zoo))
    

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      • 2018-10-30
      • 2015-12-15
      • 2020-11-24
      相关资源
      最近更新 更多