R 使用时区提取日期和时间
R extract Date and Time with Timezone
我最初就此 问了一个更简单的问题,答案很有效。但是,我现在需要对来自另一个国家/地区的数据集做同样的事情,我需要更改时区以与我的分析保持一致。
这是我的 df 中的一行:
df
Date Name Surname Amount
2015-07-24 23:15:07 John Smith 200
假设我想转换为时区 +8 小时并在不同的列中提取时间和日期。这是我想要的:
df1
Date Year Month Day Day_w Hour Minute Seconds Name Surname Amount
2015-07-24 23:15:07 2015 7 25 Saturday 07 15 07 John Smith 200
如您所见,原始日期列保持不变,但新列会自动转换时间(上午 7 点从晚上 11 点),并在必要时更改日期(在本例中为 24 点到 25 点;以及从应该是周五到周六)。
我想我可以先将整个日期列转换为新的时区,然后拆分日期和时间,但是如果时间需要,我该如何让它自动更改日期(就像我的示例中那样) ?
谢谢!
我假设你的日期是字符串格式,即 d <- "2015-07-24 23:15:07"
。
您可以使用 as.POSIXct
.
将其转换为基于您当地时区的日期
> a <- as.POSIXct(d)
> a
[1] "2015-07-24 23:15:07 MYT"
(MYT 是我当地的时区)
并且您可以在指定 tz
的另一个时区转换相同的日期时间,例如
> b <- as.POSIXct(b, tz="Japan")
> b
[1] "2015-07-24 23:15:07 JST"
因为您可以通过更改 tzone
属性将它们转换为其他时区。例如。我想将本地时间的第一个变量转换为与第二个变量相同的时区
> attr(a, "tzone") <- "Japan"
> a
[1] "2015-07-25 00:15:07 JST"
(如果要更改为本地时区,请将 tzone
设置为 ""
)
如果只想提取时间,请使用format
> format(a, "%H")
[1] "00"
或完整的日期时间
> format(a, "%Y-%m-%d %H:%M:%S")
[1] "2015-07-25 00:15:07"
> format(b, "%Y-%m-%d %H:%M:%S")
[1] "2015-07-24 23:15:07"
您可以使用包 lubridate。 revolution analytics 上有一篇关于此的文章。
library(lubridate)
library(dplyr)
df <- data.frame(Date = ymd_hms("2015-07-24 23:15:07"), Name = "John", Surname = "Smith", Amount = 200, stringsAsFactors = FALSE)
# date in UTC
df$Date
"2015-07-24 23:15:07 UTC"
# show timezone if you go to +8 hrs, from UTC / GMT to Shanghai.
# df$Date is not changed into new timezone
with_tz(df$Date, tzone = "Asia/Shanghai")
"2015-07-25 07:15:07 CST"
# create output df and for each extraction change timezone
df <- df %>% mutate(year = year(with_tz(df$Date, tzone = "Asia/Shanghai")),
month = month(with_tz(df$Date, tzone = "Asia/Shanghai")),
day = day(with_tz(df$Date, tzone = "Asia/Shanghai")),
hour = hour(with_tz(df$Date, tzone = "Asia/Shanghai")),
minute = minute(with_tz(df$Date, tzone = "Asia/Shanghai")),
second = second(with_tz(df$Date, tzone = "Asia/Shanghai")))
# print df
df
Date Name Surname Amount year month day hour minute second
1 2015-07-24 23:15:07 John Smith 200 2015 7 25 7 15 7
library(lubridate)
# For Current date and time
d = now()
d = force_tz(d, "America/Chicago")
hour(d) = hour(d)+8;
df1 = data.frame(Date = d, Year = year(d), Month = month(d), Day = day(d), Day_w = wday(d,label=T), Hour = hour(d), Minute = minute(d), Seconds = second(d), Name ="John", Surname = "Smith", Amount=200)
print(df1);
好的,我想我找到了答案。假设我的时间是以悉尼时间记录的,我想将其转换为英国时间:
#Assigns new column as date
df$Date1 <- as.POSIXct(df$Date, tz="Australia/Sydney")
#Convert it to desired time
attributes(df$Date1)$tzone <- "Europe/London"
它对我来说效果很好,所以我想我会 post 它以防其他人也需要它。
我最初就此
这是我的 df 中的一行:
df
Date Name Surname Amount
2015-07-24 23:15:07 John Smith 200
假设我想转换为时区 +8 小时并在不同的列中提取时间和日期。这是我想要的:
df1
Date Year Month Day Day_w Hour Minute Seconds Name Surname Amount
2015-07-24 23:15:07 2015 7 25 Saturday 07 15 07 John Smith 200
如您所见,原始日期列保持不变,但新列会自动转换时间(上午 7 点从晚上 11 点),并在必要时更改日期(在本例中为 24 点到 25 点;以及从应该是周五到周六)。
我想我可以先将整个日期列转换为新的时区,然后拆分日期和时间,但是如果时间需要,我该如何让它自动更改日期(就像我的示例中那样) ?
谢谢!
我假设你的日期是字符串格式,即 d <- "2015-07-24 23:15:07"
。
您可以使用 as.POSIXct
.
> a <- as.POSIXct(d)
> a
[1] "2015-07-24 23:15:07 MYT"
(MYT 是我当地的时区)
并且您可以在指定 tz
的另一个时区转换相同的日期时间,例如
> b <- as.POSIXct(b, tz="Japan")
> b
[1] "2015-07-24 23:15:07 JST"
因为您可以通过更改 tzone
属性将它们转换为其他时区。例如。我想将本地时间的第一个变量转换为与第二个变量相同的时区
> attr(a, "tzone") <- "Japan"
> a
[1] "2015-07-25 00:15:07 JST"
(如果要更改为本地时区,请将 tzone
设置为 ""
)
如果只想提取时间,请使用format
> format(a, "%H")
[1] "00"
或完整的日期时间
> format(a, "%Y-%m-%d %H:%M:%S")
[1] "2015-07-25 00:15:07"
> format(b, "%Y-%m-%d %H:%M:%S")
[1] "2015-07-24 23:15:07"
您可以使用包 lubridate。 revolution analytics 上有一篇关于此的文章。
library(lubridate)
library(dplyr)
df <- data.frame(Date = ymd_hms("2015-07-24 23:15:07"), Name = "John", Surname = "Smith", Amount = 200, stringsAsFactors = FALSE)
# date in UTC
df$Date
"2015-07-24 23:15:07 UTC"
# show timezone if you go to +8 hrs, from UTC / GMT to Shanghai.
# df$Date is not changed into new timezone
with_tz(df$Date, tzone = "Asia/Shanghai")
"2015-07-25 07:15:07 CST"
# create output df and for each extraction change timezone
df <- df %>% mutate(year = year(with_tz(df$Date, tzone = "Asia/Shanghai")),
month = month(with_tz(df$Date, tzone = "Asia/Shanghai")),
day = day(with_tz(df$Date, tzone = "Asia/Shanghai")),
hour = hour(with_tz(df$Date, tzone = "Asia/Shanghai")),
minute = minute(with_tz(df$Date, tzone = "Asia/Shanghai")),
second = second(with_tz(df$Date, tzone = "Asia/Shanghai")))
# print df
df
Date Name Surname Amount year month day hour minute second
1 2015-07-24 23:15:07 John Smith 200 2015 7 25 7 15 7
library(lubridate)
# For Current date and time
d = now()
d = force_tz(d, "America/Chicago")
hour(d) = hour(d)+8;
df1 = data.frame(Date = d, Year = year(d), Month = month(d), Day = day(d), Day_w = wday(d,label=T), Hour = hour(d), Minute = minute(d), Seconds = second(d), Name ="John", Surname = "Smith", Amount=200)
print(df1);
好的,我想我找到了答案。假设我的时间是以悉尼时间记录的,我想将其转换为英国时间:
#Assigns new column as date
df$Date1 <- as.POSIXct(df$Date, tz="Australia/Sydney")
#Convert it to desired time
attributes(df$Date1)$tzone <- "Europe/London"
它对我来说效果很好,所以我想我会 post 它以防其他人也需要它。