使用R中的lubridate从日期确定季节 [英] Determine season from Date using lubridate in R
问题描述
我有一个很大的数据集,其中的DateTime
列包含POSIXct-Values.我需要根据DateTime
列确定季节(冬季-夏季).我创建了一个在小型数据集上可以正常运行的函数,但是在大型数据集上使用时会崩溃.有人可以看到我的错误吗?
I have a very big dataset with a DateTime
Column containing POSIXct-Values. I need to determine the season (Winter - Summer) based on the DateTime
column. I've created a function which works fine on a small dataset, but crashes when I use it on the large one. Can anybody see my mistake?
我已经创建了4个函数:
I've created 4 functions:
- 3个子功能,这样我就可以进行逻辑比较和选择 使用* apply函数
- 1个确定季节的功能
- 3 subfunctions so that I can do logical comparisons and selection using *apply functions
- 1 function to determine the season
以下是功能:
require(lubridate)
# function for logical comparison (to be used in *apply)
greaterOrEqual <- function(x,y){
ifelse(x >= y,T,F)
}
# function for logical comparison (to be used in *apply)
less <- function(x,y){
ifelse(x < y,T,F)
}
# function for logical comparison (to be used in *apply)
selFromLogic <- function(VecLogic,VecValue){
VecValue[VecLogic]
}
# Main Function to determine the season
getTwoSeasons <- function(input.date) {
Winter1Start <- as.POSIXct("2000-01-01 00:00:00", tz = "UTC")
Winter1End <- as.POSIXct("2000-04-15 23:59:59", tz = "UTC")
SummerStart <- Winter1End + 1
SummerEnd <- as.POSIXct("2000-10-15 23:59:59", tz = "UTC")
Winter2Start <- SummerEnd + 1
Winter2End <- as.POSIXct("2000-12-31 00:00:00", tz = "UTC")
year(input.date) <- year(Winter1Start)
attr(input.date, "tzone") <- attr(Winter1Start, "tzone")
SeasonStart <- c(Winter1Start,SummerStart,Winter2Start)
SeasonsEnd <- c(Winter1End,SummerEnd,Winter2End)
Season_names <- as.factor(c("WinterHalfYear","SummerHalfYear","WinterHalfYear"))
Season_select <- sapply(SeasonStart, greaterOrEqual, x = input.date) & sapply(SeasonsEnd, less, x = input.date)
Season_return <- apply(Season_select,MARGIN = 1,selFromLogic,VecValue = Season_names)
return(Season_return)
}
这是测试功能的一种方法:
And here's a way to test the function:
dates <- Sys.time() + seq(0,10000,10)
getTwoSeasons(dates)
感谢您的帮助,这让我发疯了!
I would be thankful for any help, this is driving me crazy!
推荐答案
我将@Lars Arne Jordanger更为优雅的方法打包为一个函数:
I packaged @Lars Arne Jordanger's much more elegant approach into a function:
getTwoSeasons <- function(input.date){
numeric.date <- 100*month(input.date)+day(input.date)
## input Seasons upper limits in the form MMDD in the "break =" option:
cuts <- base::cut(numeric.date, breaks = c(0,415,1015,1231))
# rename the resulting groups (could've been done within cut(...levels=) if "Winter" wasn't double
levels(cuts) <- c("Winter", "Summer","Winter")
return(cuts)
}
在一些示例数据上对其进行测试似乎可以正常工作:
Testing it on some sample data seems to work fine:
getTwoSeasons(as.POSIXct("2016-01-01 12:00:00")+(0:365)*(60*60*24))
这篇关于使用R中的lubridate从日期确定季节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!