3

我正在使用data.table包,我使用了这个:

dt$date<- as.POSIXct(dt$date, tz="GMT")      (I know I can use fastPOSIXct)
2009-08-07 06:00:14
2009-08-07 06:00:15
2009-08-07 06:00:16 
2009-08-07 06:00:24

我想更改时区(有很多)并提取小时。假设我想使用 apply 功能:

f <- function(x) {
  SydneyTime<-format(x["date"], format = "%Y-%m-%d %H:%M:%OS", tz = "Australia/Sydney")
  Sy<-hour(SydneyTime)
  return(Sy)
}

mydata$SyHour <- apply(dt, 1, f)

这太慢了,我错过了什么吗?我不想保留 SydneyTime 的副本。

谢谢。

4

1 回答 1

4

You don't need to copy anything. format.Date is vectorised so you could use := to make a new column in your data.table using the data from the original column. Here is a small reproducible example:

require( data.table )
#  Seconds in the day
n <- 86400

#  Make some data
DT <- data.table( Date = as.POSIXct( Sys.time()+seq(0,2*n,by=n) , tz = "GMT") )
#                  Date
#1: 2013-08-28 21:17:10
#2: 2013-08-29 21:17:10
#3: 2013-08-30 21:17:10

#  Change the TZ
DT[ , Date2:=format( Date , tz = "Australia/Sydney")]
#                  Date               Date2
#1: 2013-08-28 21:17:10 2013-08-29 06:17:10
#2: 2013-08-29 21:17:10 2013-08-30 06:17:10
#3: 2013-08-30 21:17:10 2013-08-31 06:17:10

EDit relating to comment below

lapply is designed to be used column-wise with a data.table. To modify the column Date in-place you can do this:

DT[ , lapply( .SD , format , tz = "Australia/Sydney" ) ]

But check the meaning of .SD and .SDcols before using this on your real data.

于 2013-08-28T20:19:08.197 回答