使用R

时间:2016-04-08 14:25:31

标签: r posixct lubridate

我有一个非常大的数据集,其中DateTime列包含POSIXct-Values。我需要根据DateTime列确定季节(冬季 - 夏季)。我已经创建了一个在小数据集上工作正常的函数,但是当我在大数据集上使用它时会崩溃。任何人都可以看到我的错误吗?

我创建了4个函数:

  • 3个子功能,以便我可以进行逻辑比较和选择 使用* apply functions
  • 1确定季节的功能

以下是功能:

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)
}

这是一种测试功能的方法:

dates <- Sys.time() + seq(0,10000,10)
getTwoSeasons(dates)

我会感激任何帮助,这让我发疯了!

6 个答案:

答案 0 :(得分:2)

我将@Lars Arne Jordanger的优雅方法打包成一个功能:

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)
}

对某些样本数据进行测试似乎工作正常:

getTwoSeasons(as.POSIXct("2016-01-01 12:00:00")+(0:365)*(60*60*24))

答案 1 :(得分:2)

如果您对恢复四个季节感兴趣,可以使用以下代码:

library(lubridate)
getSeason <- 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,319,0620,0921,1220,1231)) 
  # rename the resulting groups (could've been done within cut(...levels=) if "Winter" wasn't double
  levels(cuts) <- c("Winter","Spring","Summer","Fall","Winter")
  return(cuts)
}

单元测试:

getSeason(as.POSIXct("2016-01-01 12:00:00")+(0:365)*(60*60*24))

答案 2 :(得分:2)

为了完整起见,值得注意的是lubridate现在有四分之一(和一个学期)的功能。 quarter将年份分为四分之一,将semester分成两半:

quarter(x, with_year = FALSE, fiscal_start = 1)
semester(x, with_year = FALSE)

有关详情,请参阅:https://www.rdocumentation.org/packages/lubridate/versions/1.7.4/topics/quarter

答案 3 :(得分:1)

经过几个小时的调试后,我发现了自己的错误,实在是太荒谬了:

如果找不到DateTimeValue的季节,apply返回list - 对象而不是vector(当DateTime值等于2000-12-31 00:00:00时就是这种情况) 。返回列表创建了计算时间和所描述的崩溃的过度增加。这是固定代码:

# input date and return 2 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("2001-01-01 00:00:01", tz = "UTC")

  SeasonStart <- c(Winter1Start,SummerStart,Winter2Start)
  SeasonsEnd <- c(Winter1End,SummerEnd,Winter2End)
  Season_names <- factor(c("WinterHalf","SummerHalf","WinterHalf"))

  year(input.date) <- year(Winter1Start)
  attr(input.date, "tzone") <- attr(Winter1Start, "tzone")

  Season_selectStart <- vapply(X = SeasonStart,function(x,y){x <= input.date},FUN.VALUE = logical(length(input.date)),y = input.date)
  Season_selectEnd   <- vapply(X = SeasonsEnd,function(x,y){x > input.date},FUN.VALUE = logical(length(input.date)),y = input.date)
  Season_selectBoth  <- Season_selectStart & Season_selectEnd
  Season_return <- apply(Season_selectBoth,MARGIN = 1,function(x,y){y[x]}, y = Season_names)
  return(Season_return)
}

“sub”功能现已集成在main函数中,两个sapply函数已替换为vapply

PS:时区仍然存在问题,因为c()将时区剥离。我修复它时会更新代码。

答案 4 :(得分:1)

也可以使用以下策略:基本观察是 substr可以提取我们需要的月和日信息 决定是夏天还是冬天。然后想法将其转换为 month.date 形式的数字,以及夏天的测试 归结为数字大于4.15但小于10.16。

下面的示例显示了如何在日期向量中完成此操作 首先转换为所描述的替代演示 上面,然后是一个向量,告诉它是夏天“真”或冬天 将基于此创建“FALSE”。

DateTime <- as.POSIXct(x  = "2000-01-01 00:00:00",
                       tz = "UTC") +
    (0:1000)*(60*60*24)

DateTime_2 <- as.numeric(paste(
    substr(x = DateTime,
           start = 6,
           stop = 7),
    substr(x = DateTime,
           start = 9,
           stop = 10),
    sep = "."))

.season <- (DateTime_2 > 4.15) & (DateTime_2 < 10.16)

答案 5 :(得分:0)

使用POSXlt代替POSXct。

我根据自己所用季节的定义制定了自己的功能。我为非s年创建了名为 normal 的矢量,为leap年创建了名为 leap 的矢量,每个季节名称均重复编号。从1月1日开始出现。并创建了以下功能。

SEASON <- function(datee){
  
  datee <- as.POSIXlt(datee)
  season <- vector()
  normal <- rep(c("Winter","Spring","Summer","Monsoon","Autumn","Winter"), c(46,44,91,77,76,31))
  leap <- rep(c("Winter","Spring","Summer","Monsoon","Autumn","Winter"), c(46,45,91,77,76,31))

  
  if(leap_year(year(datee)) == FALSE){
    season <- normal[datee$yday+1]
  } else {
    season <- leap[datee$yday+1]
  }
  return(season)
}

让我们测试一下某些数据集。

Dates <- seq(as.POSIXct("2000-01-01"), as.POSIXct("2010-01-01"), by= "day")
sapply(Dates, SEASON)

有效。