我會創建這是一年中的天(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
我希望這個對你有用!
使用'dput'或'head'來發布您的某些數據框以獲得特定答案。請參閱:http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – harkmug