as.POSIXlt plotting weekdays in one console but not another--locale matching issues

16 views Asked by At

Background: I am trying to paste a date and a time column from a dataframe and run it through as.POSIXlt to create a datetime, which, when plotted, will show the day of the week on the x axis instead of Feb 1st 00:00:00, etc.

I am making a script to create this plot that has to work in all consoles, obviously, and I used it to create this plot in a base R console-- ie the R software, no special environment. enter image description here

It works beautifully.

I can't share the whole code because of Coursera's academic honesty policy, but here's the code I used to merge the date and the time:

power1<-mutate(power, dt = as.POSIXlt(paste(power$Date, power$Time), format = "%Y-%m-%d %H:%M:%S"))
head(power1$dt)
[1] "2007-02-01 00:00:00 PST" "2007-02-01 00:01:00 PST"
[3] "2007-02-01 00:02:00 PST" "2007-02-01 00:03:00 PST"
[5] "2007-02-01 00:04:00 PST" "2007-02-01 00:05:00 PST"

["power" is the data frame I'm working with]

Move it to R Studio (Posit) cloud, and head(power1$dt) looks like:

head(power1$dt)
[1] "2007-02-01 00:00:00 UTC" "2007-02-01 00:01:00 UTC"
[3] "2007-02-01 00:02:00 UTC" "2007-02-01 00:03:00 UTC"
[5] "2007-02-01 00:04:00 UTC" "2007-02-01 00:05:00 UTC"

, so I rerun the command to specify PST time zone ("America/Los_Angeles"). That makes the output of head(power1$dt) look the same in Posit cloud as in my base R console, but the graph the script produces looks like this, which is what I don't want:

enter image description here

I understand that locale can influence how the as.POSIXlt (and other commands) parse and store datetime information and I found my locale in the base R console:

 Sys.getlocale("LC_TIME")
[1] "English_United States.utf8"

and my locale in Posit cloud was:

Sys.getlocale("LC_TIME")
[1] "C.UTF-8"

When I tried to set the locale in Posit cloud to what my base R console was saying, it said this:

 Sys.setlocale("LC_TIME", "English_United States.utf8")
[1] ""
Warning message:
In Sys.setlocale("LC_TIME", "English_United States.utf8") :
  OS reports request to set locale to "English_United States.utf8" cannot be honored

I noticed the locale from my base R was formatted weird, so I tried to change the Posit cloud locale to something that looked more familiar that I thought would be more or less the same:

 Sys.setlocale("LC_TIME", "en_US.UTF-8")
[1] "en_US.UTF-8"

And when I called the function that produced the plot, it looked like the second picture again.

How do I change the locale in my posit cloud project so that the plot turns out the same as in my base R console?

Better yet, is there an easier way to just make the graph say what I want it to say, without having to worry about locales at all?

It may be worth it to mention my base R console is running 4.2.0, and Posit cloud runs whatever is the most recent.

0

There are 0 answers