【问题标题】:Date vs. Temperature Data Frame: Finding max temperature each day in R日期与温度数据框:在 R 中查找每天的最高温度
【发布时间】:2013-06-18 20:36:20
【问题描述】:

我在 R 中有一个数据框,我从 R 中的 csv 上传,并试图找到每天的最高温度。 data.frame 的格式设置为 col(1) 是日期(YYYY-MM-DD HH:mm 格式),而 col(2) 是该日期/时间的温度。我尝试将数据分类为子集,自上而下工作(年、当年的月、那些月的天),但发现它非常复杂。

这是数据框的示例:

                 Date Unit Temp
1 2012-10-21 21:14:00    C 82.5
2 2012-10-21 21:34:00    C 37.5
3 2012-10-21 21:54:00    C 20.0
4 2012-10-21 22:14:00    C 26.5
5 2012-10-21 22:34:00    C 20.0
6 2012-10-21 22:54:00    C 19.0

【问题讨论】:

标签: r date sorting dataframe


【解决方案1】:

xts 包中的 apply.daily 函数完全符合您的要求。

install.packages("xts")
require('xts')

tmp <- data.frame(Date = seq(as.POSIXct("2013-06-18 10:00"),
    length.out = 100, by = "6 hours"),
    Unit = "C",
    Temp = rnorm(n = 100, mean = 20, sd = 5)) # thanks to dickoa for this code

head(tmp)
data <- xts(x=tmp[ ,3], order.by=tmp[,1])
attr(data, 'Unit') <- tmp[,'Unit']
attr(data, 'Unit')

dMax <- apply.daily(data, max)
head(dMax)

【讨论】:

    【解决方案2】:

    我将创建一个表示年份 (DoY) 的列,然后使用 aggregate 函数查找每个 DoY 的最高温度。

    例如,假设您的 data.frame 名为 Data,而 Data 有两列:第一列名为“日期”,第二列名为“温度”。我会做以下事情:

    Data[,"DoY"] <- format.Date(Data[,"Date"], format="%j") #make sure that Data[,"Date"] is already in a recognizable format-- e.g., see as.POSIXct()
    MaxTemps <- aggregate(Data[,"Temperature"], by=list(Data[,"DoY"]), FUN=max) # can add na.rm=TRUE if there are missing values
    

    MaxTemps 应包含每天观察到的最高温度。但是,如果您的数据集中有多个年份,例如,第 169 天(今天)重复多次(例如,今天和 1 年前),您可以执行以下操作:

    Data[,"DoY"] <- format.Date(Data[,"Date"], format="%Y_%j") #notice the date format, which will be unique for all combinations of year and day of year.
    MaxTemps <- aggregate(Data[,"Temperature"], by=list(Data[,"DoY"]), FUN=max) # can add na.rm=TRUE if there are missing values
    

    我希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      我假设您有一个名为df 的数据框,其中包含变量datetemp。 此代码未经测试,但如果运气好的话,它可能会起作用。

      library(lubridate)
      df$justday <- floor_date(df$date, "day")
      
      # for just the maxima, you could use this:
      tapply(df$temp, df$justday, max)
      
      # if you would rather have the results in a data frame, use this:
      aggregate(temp ~ justday, data=df)
      

      【讨论】:

        【解决方案4】:

        没有可复制的示例并非易事。

        话虽如此,您可以使用lubridate(日期管理)和plyr(拆分应用)来解决这个问题。

        让我们先创建一个类似于你的数据

        set.seed(123)
        tmp <- data.frame(Date = seq(as.POSIXct("2013-06-18 10:00"),
                          length.out = 100, by = "6 hours"),
                          Unit = "C",
                          Temp = rnorm(n = 100, mean = 20, sd = 5))
        str(tmp)
        ## 'data.frame':    100 obs. of  3 variables:
        ##  $ Date: POSIXct, format: "2013-06-18 10:00:00" ...
        ##  $ Unit: Factor w/ 1 level "C": 1 1 1 1 1 1 1 1 1 1 ...
        ##  $ Temp: num  17.2 18.8 27.8 20.4 20.6 ...
        
        
        write.csv(tmp, "/tmp/tmp.csv", row.names = FALSE)
        rm(tmp)
        

        现在我们可以计算最大值

        require(lubridate)
        require(plyr)
        
        ### NULL is to not import the second column which is the unit 
        tmp <- read.csv("/tmp/tmp.csv",
                        colClasses = c("POSIXct", "NULL", "numeric"))
        
        
        tmp <- transform(tmp, jday = yday(Date))
        
        
        ddply(tmp, .(jday), summarise, max_temp = max(Temp))
        
        ##    jday max_temp
        ## 1   169   27.794
        ## 2   170   28.575
        ## 3   171   26.120
        ## 4   172   22.004
        ## 5   173   28.935
        ## 6   174   18.910
        ## 7   175   24.189
        ## 8   176   26.269
        ## 9   177   24.476
        ## 10  178   23.443
        ## 11  179   18.960
        ## 12  180   30.845
        ## 13  181   23.900
        ## 14  182   26.843
        ## 15  183   27.582
        ## 16  184   21.898
        ...................
        

        【讨论】:

        • 对不起,我应该从我的数据框中添加一些样本,现在让我这样做。我是 R 和 stackflow 的新手!
        • @user2498712 我根据您的数据结构更新我的答案。试试看是否有效
        • 我收到此错误消息:> ddply(tmp, .(jday), summarise, max_temp = max(Temp)) 属性错误(out)
        • @user2498712 我创建了一个与您的相似的完整可复制示例。它应该可以正常工作
        • 啊!不幸的是,它没有用。它现在告诉我: as.POSIXlt.character(x, tz, ...) 中的错误:字符串不是标准的明确格式
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-13
        • 1970-01-01
        • 2020-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-27
        相关资源
        最近更新 更多