如何更改R中的默认时区?

发布于 2024-11-16 01:22:50 字数 84 浏览 6 评论 0原文

如何更改 R 中的默认时区? 我正在处理时间序列。我的所有时间序列都是在 UTC 时区中定义的,但如果我打印日期,它总是在 CET/CEST 时区中完成。

How can I change the default timezone in R?
I'm working with time series. All my time series are defined in UTC time zone, but if I print a date it is always done in CET/CEST time zone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白衬杉格子梦 2024-11-23 01:22:50

另一种无需更改整个计算机时间的方法是使用 setenv 命令,如下所示: Sys.setenv(TZ='GMT')

Another way to do it, without changing the whole computer time is using the setenv command like this : Sys.setenv(TZ='GMT')

奈何桥上唱咆哮 2024-11-23 01:22:50

请参阅这篇关于在 R 中更改时区的好文章:

http://blog.revolutionanalytics.com/2009 /06/converting-time-zones.html

很快(以防链接将来不可用):

# your time string
pb.txt <- "2009-06-03 19:30"
# convert it to R object for London time zone
pb.date <- as.POSIXct(pb.txt, tz="Europe/London")
# convert it to PDT time zone
format(pb.date, tz="America/Los_Angeles",usetz=TRUE)
[1] "2009-06-03 11:30:00 PDT"

# can be also done for many date at once
d <- c("2009-03-07 12:00", "2009-03-08 12:00", "2009-03-28 12:00", "2009-03-29 12:00", "2009-10-24 12:00", "2009-10-25 12:00", "2009-10-31 12:00", "2009-11-01 12:00")
t1 <- as.POSIXct(d,"America/Los_Angeles")
cbind(US=format(t1),UK=format(t1,tz="Europe/London"))

     US                    UK                   
[1,] "2009-03-07 12:00:00" "2009-03-07 20:00:00"
[2,] "2009-03-08 12:00:00" "2009-03-08 19:00:00"
[3,] "2009-03-28 12:00:00" "2009-03-28 19:00:00"
[4,] "2009-03-29 12:00:00" "2009-03-29 20:00:00"
[5,] "2009-10-24 12:00:00" "2009-10-24 20:00:00"
[6,] "2009-10-25 12:00:00" "2009-10-25 19:00:00"
[7,] "2009-10-31 12:00:00" "2009-10-31 19:00:00"
[8,] "2009-11-01 12:00:00" "2009-11-01 20:00:00"

See this good article on changing time zone in R:

http://blog.revolutionanalytics.com/2009/06/converting-time-zones.html

Shortly (in case the link will be unavailable in the future):

# your time string
pb.txt <- "2009-06-03 19:30"
# convert it to R object for London time zone
pb.date <- as.POSIXct(pb.txt, tz="Europe/London")
# convert it to PDT time zone
format(pb.date, tz="America/Los_Angeles",usetz=TRUE)
[1] "2009-06-03 11:30:00 PDT"

# can be also done for many date at once
d <- c("2009-03-07 12:00", "2009-03-08 12:00", "2009-03-28 12:00", "2009-03-29 12:00", "2009-10-24 12:00", "2009-10-25 12:00", "2009-10-31 12:00", "2009-11-01 12:00")
t1 <- as.POSIXct(d,"America/Los_Angeles")
cbind(US=format(t1),UK=format(t1,tz="Europe/London"))

     US                    UK                   
[1,] "2009-03-07 12:00:00" "2009-03-07 20:00:00"
[2,] "2009-03-08 12:00:00" "2009-03-08 19:00:00"
[3,] "2009-03-28 12:00:00" "2009-03-28 19:00:00"
[4,] "2009-03-29 12:00:00" "2009-03-29 20:00:00"
[5,] "2009-10-24 12:00:00" "2009-10-24 20:00:00"
[6,] "2009-10-25 12:00:00" "2009-10-25 19:00:00"
[7,] "2009-10-31 12:00:00" "2009-10-31 19:00:00"
[8,] "2009-11-01 12:00:00" "2009-11-01 20:00:00"
救星 2024-11-23 01:22:50

什么操作系统?

一般来说,请参阅 help(Startup),因为您可以通过 .Renviron 及其站点范围的变体设置值。

但是你可能应该为你的机器整体设置这个,在 Linux 下可能会改变文件 /etc/timezone,而在 Windows 上你需要设置一个系统范围的环境变量 TZ< /代码>。

最后,如果您的日期和时间的格式化显示显示CET/CEST,这可能已被设置为系统默认值,您的问题实际上是如何在 R 对象中正确设置 UTC 时间。

What operating system?

In general, see help(Startup) as you can set values via .Renviron and its site-wide variant.

But you should probably set this for your machine as a whole, which under Linux may alter the file /etc/timezone, and on Windows you'd set a system-wide environment variable TZ.

Lastly, if your formatted display of dates and time shows CET/CEST, this may already be set as a system default and your question really is how to set your UTC times correctly in your R objects.

兰花执着 2024-11-23 01:22:50

我发现 @Dirk 的答案对 Ubuntu 非常有用,所以我想我会对其进行扩展。

help(Startup) 中我们看到环境变量是由 Renviron.site 文件设置的:

除非在命令行中给出 --no-environ,否则 R 会搜索站点和用户文件来处理以设置环境变量。站点文件的名称是环境变量R_ENVIRON所指向的;如果未设置,则使用“R_HOME/etc/Renviron.site”

我们可以使用函数 R.home() 找到 R_HOME 的路径,该函数在我的case 返回:

> R.home()
[1] "/usr/lib/R"

因此,在 /usr/lib/R/etc/ 中(对我来说)找到了 Renviron.site 文件。

只需打开此文件,然后插入行:

TZ="UTC"

或类似内容。

I found @Dirk's answer very useful for Ubuntu, so I thought I would expand on it.

From help(Startup) we see that environment variables are set by the Renviron.site file:

Unless --no-environ was given on the command line, R searches for site and user files to process for setting environment variables. The name of the site file is the one pointed to by the environment variable R_ENVIRON; if this is unset, ‘R_HOME/etc/Renviron.site’ is used

We can find the path to R_HOME by using the function R.home(), which in my case returns:

> R.home()
[1] "/usr/lib/R"

Therefore, the Renviron.site file is found (for me) in /usr/lib/R/etc/.

Simply open this file, and insert the line:

TZ="UTC"

or similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文