change time locale for R
For Windows:
Sys.setlocale("LC_TIME", "English")
How do you convert dates/times from one time zone to another in R?
First, convert the London time to a POSIXct
object:
pb.txt <- "2009-06-03 19:30"
pb.date <- as.POSIXct(pb.txt, tz="Europe/London")
Then use format
to print the date in another time zone:
> format(pb.date, tz="America/Los_Angeles",usetz=TRUE)
[1] "2009-06-03 11:30:00 PDT"
There are some tricks to finding the right time zone identifier to use. More details in this post at the Revolutions blog: Converting time zones in R: tips, tricks and pitfalls
change time zone in R without it returning to original time zone
as.Date
unfortunately defaults to UTC as noted in the documentation, so you can manually specify the timezone to get it to work. Alternatively, you can use the date()
extractor from lubridate
which is better at respecting timezones.
df <- data.frame(c("2018-09-28 00:00:00Z","2018-09-29 01:00:00Z","2018-09-30 10:00:00Z"))
names(df) <- "startTime"
df$startTime <- as.POSIXct(df$startTime, tz="Etc/UTC")
attributes(df$startTime)$tzone <- "America/New_York"
as.Date(df$startTime, tz="America/New_York")
#> [1] "2018-09-27" "2018-09-28" "2018-09-30"
lubridate::date(df$startTime)
#> [1] "2018-09-27" "2018-09-28" "2018-09-30"
Created on 2018-09-25 by the reprex package (v0.2.0).
How to set the default language of date in R
OK, as a Q&A site, it seems an answer is required. From your description, it appears to be the issue of your locales. Read ?locales
for more.
You can have a test with this (read ?strptime
for various format, and pay special attention to those sensitive to locales):
format(Sys.Date(), format = "%Y-%b-%d")
# [1] "2016- 9月-06"
The output has the month in Chinese. If I want to change the display, I need to set "LC_TIME"
locale to "C"
:
Sys.setlocale("LC_TIME", "C")
Then it is OK:
format(Sys.Date(), "%Y-%b-%d")
# [1] "2016-Sep-06"
Every time you start a new R session, you get back to native setting. Should you want a permanent change, put
.First <- function() {
Sys.setlocale("LC_TIME", "C")
}
in the $(R RHOME)/etc/Rprofile.site
file. Read ?Startup
for how to customize R startup and the use of .First
.
R Change from stardard UTC time zone to multiple local time zones
Multiple problems here:
format
(and other time-related functions) only takes a length-1 argument fortz
;- time zones recognized by R do not include the popular
"CST"
,"PST"
, etc.
To fix the first, the use of Map
or mapply
will suffice.
The second requires a little more research, unfortunately. Zones like "PST"
and such, though popular in at least the US if not other countries, are not valid time zone strings (ref: CCTZ, a C++ library for translating between time zones, says so). Neither are "GMT-7"
, et al, though the latter can be faked by prepending with Etc/
, as in: "Etc/GMT-7"
. Or you can go with the alternatives of "America/New_York"
or "US/Eastern"
.
df$time_zone <- c("US/Eastern", "US/Pacific", "US/Central", "US/Eastern")
df
# day time_zone
# 1 2018-12-06 15:40:29 US/Eastern
# 2 2018-12-06 15:25:28 US/Pacific
# 3 2018-12-06 15:25:28 US/Central
# 4 2018-12-06 14:09:09 US/Eastern
mapply(format, df$day, tz = "GMT")
# [1] "2018-12-06 15:40:29" "2018-12-06 15:25:28" "2018-12-06 15:25:28"
# [4] "2018-12-06 14:09:09"
mapply(format, df$day, tz = df$time_zone)
# [1] "2018-12-06 10:40:29" "2018-12-06 07:25:28" "2018-12-06 09:25:28"
# [4] "2018-12-06 09:09:09"
All of the immediately-recognizable formats for R's time zones are found in a 594-element vector:
str(OlsonNames())
# chr [1:592] "Africa/Abidjan" "Africa/Accra" "Africa/Addis_Ababa" ...
# - attr(*, "Version")= chr "2018e"
set.seed(2)
sample(OlsonNames(), size=8)
# [1] "America/El_Salvador" "Etc/GMT+8" "Atlantic/Madeira"
# [4] "America/Creston" "Pacific/Port_Moresby" "Pacific/Ponape"
# [7] "America/Atka" "GB-Eire"
grep("US/", OlsonNames(), value = TRUE)
# [1] "US/Alaska" "US/Aleutian" "US/Arizona"
# [4] "US/Central" "US/East-Indiana" "US/Eastern"
# [7] "US/Hawaii" "US/Indiana-Starke" "US/Michigan"
# [10] "US/Mountain" "US/Pacific" "US/Pacific-New"
# [13] "US/Samoa"
In this example, you'll see one of the alternatives you can use: "Etc/GMT+8"
. Notice that +
is to the west of the prime meridian, so
mapply(format, df$day, tz = "US/Eastern")
# [1] "2018-12-06 10:40:29" "2018-12-06 10:25:28" "2018-12-06 10:25:28"
# [4] "2018-12-06 09:09:09"
mapply(format, df$day, tz = "Etc/GMT+5")
# [1] "2018-12-06 10:40:29" "2018-12-06 10:25:28" "2018-12-06 10:25:28"
# [4] "2018-12-06 09:09:09"
Caveat emptor: using "US/Eastern"
should take into account daylight savings where appropriate; "Etc/GMT+5"
does not, I believe.
How to change the default time zone in R?
Another way to do it, without changing the whole computer time is using the setenv command like this : Sys.setenv(TZ='GMT')
Using dplyr::if_else() in R to change the time zone of POSIXct timestamps based on value of another variable
This doesn't answer your original question about why with_tz
doesn't work with if_else
but here is one workaround. We subtract 4 hours (difference between UTC and EST) where tz == "UTC"
.
library(dplyr)
library(lubridate)
x %>% mutate(ts_New = if_else(tz == "UTC", ts - hours(4), ts))
# ts tz ts_New
#1 2017-04-27 13:44:00 UTC 2017-04-27 09:44:00
#2 2017-03-10 12:22:00 EST 2017-03-10 12:22:00
#3 2017-03-22 10:24:00 UTC 2017-03-22 06:24:00
Or in base R
x$ts_New <- x$ts
inds <- x$tz == "UTC"
x$ts_New[inds] <- x$ts_New[inds] - 4 * 60 * 60
Related Topics
How to Use Outlier Tests in R Code
Ggplot2: Is There a Fix for Jagged, Poor-Quality Text Produced by Geom_Text()
Remove Rows Where All Variables Are Na Using Dplyr
Extracting Coefficient Variable Names from Glmnet into a Data.Frame
How to Add Another Layer/New Series to a Ggplot
Ggplot Geom_Point() with Colors Based on Specific, Discrete Values
Change Color of Leaflet Marker
How to Add an Inset (Subplot) to "Topright" of an R Plot
Knitr (R) - How Not to Embed Images in the HTML File
Replace All Na with False in Selected Columns in R
R Draw All Axis Labels (Prevent Some from Being Skipped)
Using Annotate to Add Different Annotations to Different Facets