当平均时,dateTime从POSIXct变为因子 - R.

时间:2013-05-02 17:43:12

标签: r posixct

我有以下示例:

begin_date = as.POSIXlt("1990-01-01", tz = "GMT")
# 30 year dataset
dat = data.frame(dateTime = begin_date + (0:(24*30*20)) * (1800))
dat = within(dat,{speed = runif(length(dateTime), 1, 10)
})

我希望计算每小时的平均值,所以我使用:

data <- aggregate(dat[colnames(dat)[2:length(colnames(dat))]],
                  list(dateTime = cut(dat$dateTime,breaks = "hour")),
                  mean,
                  na.rm = TRUE)

但是,dateTime列的类已从POSIXct更改为factor:

> class(dat$dateTime)
[1] "POSIXct" "POSIXt" 
> class(data$dateTime)
[1] "factor"

为什么会出现这种情况,如何将其更改为POSIXct?

2 个答案:

答案 0 :(得分:1)

 data$dateTime <- as.POSIXct(as.character( data$dateTime)) 

答案 1 :(得分:1)

您可以使用seqfindInterval

制作每小时的分档
rng <- range(dat$dateTime)
rng[1] <- rng[1] - (as.numeric(rng[1]) %% 3600) #subtract remainder after div by 3600    secs
hrs <- seq(rng[1],rng[2],by="hour") #sequence by hour
bins <- hrs[findInterval(dat$dateTime, hrs)] #identify the start of hourly interval for each row in dat

然后使用bins代替cut

data <- aggregate(dat[colnames(dat)[2:length(colnames(dat))]],
                  list(dateTime = bins),
                  mean,
                  na.rm = TRUE)

> class(dat$dateTime)
[1] "POSIXct" "POSIXt" 

> class(data$dateTime)
[1] "POSIXct" "POSIXt"