Reputation: 572
I'm confused by the following behavior.
> tmpString <- Sys.time()
> tmpString
[1] "2021-06-17 11:50:52 EDT"
> gsub(" ", "_", tmpString)
[1] "2021-06-17_11:50:52"
> gsub(" ", "_", tmpString, fixed=TRUE)
[1] "2021-06-17_11:50:52"
Why is the " EDT" at the end of tmpString being lost?
For completeness,
> R.version
_
platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 3
minor 6.3
year 2020
month 02
day 29
svn rev 77875
language R
version.string R version 3.6.3 (2020-02-29)
nickname Holding the Windsock
>
Upvotes: 1
Views: 93
Reputation: 269481
tmpString
is not a string. As mentioned in the comments it is a POSIXct object which internally is the number of seconds since the UNIX Epoch and it is the print
function which shows it with a time zone. Better to call it tmp
instead of tmpString
and then explicitly use format
to convert it to a string. (Below we used sub
to replace the first space but use gsub
as in the question if you want both spaces replaced.)
tmp <- Sys.time()
sub(" ", "_", format(tmp, usetz = TRUE))
## [1] "2021-06-17_12:39:51 EDT"
Also note the existence of format_ISO8601
which you might prefer:
format_ISO8601(tmp, usetz = TRUE)
## [1] "2021-06-17T12:39:51-0400"
Upvotes: 1