【问题标题】:apply.monthly won't work with function (xts and POSIXct files)apply.monthly 不适用于函数(xts 和 POSIXct 文件)
【发布时间】:2017-10-17 05:41:42
【问题描述】:

我最近开始在我的硕士论文中使用 R 进行编码,并且仍在学习。

为了处理一些气象数据,我尝试创建一个函数。 原始文件是包含日期和降雨量的 csv 文件。 我提取了日期并创建了一个包含所有日期的POSIXct 对象,然后创建了一个包含我的日期和降雨量的xts 对象。

mesdonnees <- mesdonnees[26000:30000,]

require('xts')

# Extract characters and define as S....
Syear <- substr(mesdonnees$time, 1, 4)
Smonth <- substr(mesdonnees$time, 6,7)
Sday <- substr(mesdonnees$time, 9, 10)
Shour <- substr(mesdonnees$time, 12, 13)
Sminutes <- substr(mesdonnees$time, 15, 16)

#Gather all parts and use "-" as sep
datetext <- paste(Syear, Smonth, Sday, Shour, Sminutes, sep="-")
#define format of each part of the string
formatdate <- as.POSIXct(datetext, format="%Y-%m-%d-%H-%M", tz = "GMT")

xtsdata <- xts(mesdonnees[,2:3], order.by = formatdate, header = TRUE)

然后我编写了一个函数,它将对 difftime 的值求和,并在 difftime 为> 20 分钟

myfun <- function(CHANGE){       # CHANGE = formatdate or xtsdata
x <- as.difftime(0,format = "%M", units = "mins")
y <- as.difftime(0,format = "%M", units = "mins")
for (i in 2:length(CHANGE)) {
    if (difftime(CHANGE[i],CHANGE[i-1],units = "mins") <= as.difftime(20,format = "%M",units = "mins")) {
    x <- x + difftime(CHANGE[i],CHANGE[i-1],units = "mins")
    } else {
    x <- x + 20
    y <- y + difftime(CHANGE[i],CHANGE[i-1],units = "mins") - 20
    }
   }
return(list(paste("x = ",x , sep = ""),paste("y = ",y , sep = "")))
}

这个函数在使用“as.POSIXct”对象(格式日期)时工作得很好,但当我尝试使用“xts”对象(xtsdata)运行它时就不起作用了。这样做时,我收到以下错误:do not know how to convert 'x' to class “POSIXlt”。为什么x要改成POSIXct是时差呢?

下一步,我也无法开始工作,使用 apply.monthly 到这个函数,使用 `apply.monthly(xtsdata,myfun)。

目前,当尝试使用apply.monthly(formatdate,myfun)我得到以下错误:Error in if (difftime(CHANGE[i], CHANGE[i - 1], units = "mins") <= as.difftime(20, : missing value where TRUE/FALSE needed

在尝试apply.monthly(xtsdata,myfun) 时出现以下错误:Error in as.POSIXlt.default(x, tz, ...) : do not know how to convert 'x' to class “POSIXlt”

任何关于如何让我的功能正常工作以及如何应用apply.monthly 的建议都会有很大帮助。 感谢您的帮助!


编辑以获取更多详细信息:(sid:气象数据的标识,我不需要该列),时间 = 日期,值 = 降雨量

> head(mesdonnees)
      sid                time value
26000 100 2010-07-14 11:50:00     0
26001 100 2010-07-14 12:10:00     0
26002 100 2010-07-14 12:20:00     0
26003 100 2010-07-14 12:30:00     0
26004 100 2010-07-14 12:41:00     0
26005 100 2010-07-14 12:50:00     0

> head(formatdate)
[1] "2010-07-14 11:50:00 GMT" "2010-07-14 12:10:00 GMT"
[3] "2010-07-14 12:20:00 GMT" "2010-07-14 12:30:00 GMT"
[5] "2010-07-14 12:41:00 GMT" "2010-07-14 12:50:00 GMT"

> head(xtsdata)
                    time                  value
2010-07-14 11:50:00 "2010-07-14 11:50:00" " 0.00"
2010-07-14 12:10:00 "2010-07-14 12:10:00" " 0.00"
2010-07-14 12:20:00 "2010-07-14 12:20:00" " 0.00"
2010-07-14 12:30:00 "2010-07-14 12:30:00" " 0.00"

我必须根据这些数据(mesdonnees)估算每月的降雨量。初始数据在精确时间给出“即时降水”,我应该每 10 分钟有一个值。但是,事实并非如此,有时直到几天后我才知道值(并且没有NA 行来注意缺失值)。 我希望这个解释更好?

【问题讨论】:

  • 您能做到head(mesdonnees) 以便我们了解数据的外观吗?您要解决的根本问题是什么?当你写“处理一些气象数据”时,你想到的是什么样的处理?

标签: r apply xts posixct


【解决方案1】:

您可能可以将 CSV 直接读取到 xts 或 zoo 对象中。但是您没有指定 CSV 中数据的格式,所以我的回答会假设 mesdonnees 是一个 data.frame。

由于您不需要sid 列,因此创建 xts 对象所需要做的就是:

xtsdata <- xts(mesdonnees$value, as.POSIXct(mesdonnees$time))

如果mesdonnees$time 是一个因素,您需要先将其转换为字符。

xtsdata <- xts(mesdonnees$value, as.POSIXct(as.character(mesdonnees$time)))

然后,您可以调用apply.monthly(xtsdata, ...),使用任何您想使用的函数来按月创建汇总统计信息。例如:

apply.monthly(xtsdata, quantile)

【讨论】:

  • 您好,感谢您的建议!虽然它在xts 创建中节省了相当多的写作,但我仍然无法将difftime 函数用于xts 对象(R 表示它无法将'x' 转换为as.POSIXlt`,尽管它可以识别日期格式
  • @legardien:不清楚为什么需要使用difftime
  • 我需要difftime 来确保两行之间的差异不超过 20 分钟(我在函数中使用了它)。最后,我收到了有关数据的其他信息以及有关单位的一些更正,这意味着我可以做一个基本的apply.monthly(xtsdata,mean) 并使用它。感谢您提供xtscreation 提示,它使我的代码更加简洁明了。
猜你喜欢
  • 1970-01-01
  • 2021-11-20
  • 2015-05-08
  • 2013-07-08
  • 2018-01-21
  • 2013-09-22
  • 2018-08-24
  • 1970-01-01
相关资源
最近更新 更多