【问题标题】:issue in dm2seasonal function in RR中的dm2seasonal函数问题
【发布时间】:2020-11-05 18:58:12
【问题描述】:

我正在尝试使用函数dm2seasonal 在 R 中按季节计算累积降水量。 例如,这是我的动物园每日降水站列表中第一个元素的str

(例如,由于字符限制,我没有包括 dput(listDF_zoo[[1]])

> str(listDF_zoo[[1]])
‘zoo’ series from 1915-11-01 to 1945-11-01
  Data: num [1:9382] 0 0 0 0 0 18.5 9.4 0 0 10.4 ...
  Index:  Date[1:9382], format: "1915-11-01" "1915-11-02" "1915-11-03" "1915-11-04" "1915-11-05" "1915-11-06" "1915-11-07" ...

我正在使用以下代码:

#Cumulative precipitation in spring (March 1 to May 31)
SpringCumMean <- lapply(listDF_zoo,function(x){dm2seasonal(x, season ="MAM", FUN=sum)})

但我收到以下错误:

> SpringCumMean <- lapply(listDF_zoo,function(x){dm2seasonal(x, season ="MAM", FUN=sum)})
 Show Traceback
 
 Rerun with Debug
 Error in aggregate.data.frame(as.data.frame(x), ...) : 
  no rows to aggregate In addition: Warning message:
In extract.zoo(x = x, trgt = season) :
  There were no elements of 'x' within 'trgt'

我不明白为什么说元素没有行...

有什么办法吗?

【问题讨论】:

  • 请显示一个有效的dm2seasonal 的非重复调用。方法似乎依赖于aggregate,由于缺少观察结果,它可以返回no rows to aggregate
  • 我知道我在列表的第 46 元素中遇到了错误。是否可以跳过循环中的错误以避免停止循环?

标签: r list lapply zoo


【解决方案1】:

由于数据在列表中的一个元素上似乎是特定于数据的,因此请考虑tryCatch,您在错误实例上有条件地返回NULL(或任何其他对象或值)。

SpringCumMean <- lapply(listDF_zoo,function(x) {
                         tryCatch({
                              dm2seasonal(x, season ="MAM", FUN=sum)
                         }, error = function(e) NULL)
                 })

如果您需要过滤掉NULL 元素,请考虑Filter。但是这样做,SpringCumMean 将不再保持与listDF_zoo 相同的长度。

SpringCumMean <- Filter(length, SpringCumMean)

【讨论】:

    猜你喜欢
    • 2020-09-07
    • 2018-04-18
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多