-1

I am completely new to this but already figured out how to extract the data I need from an xml file. However, one of the variables I want is the date and time as Posixct. I used the following to extract:

datax<-xmlParse("myfile.xml") 
data<-xmlToList(datax)
date<-as.vector(unlist(data$.attrs[5]))

And I got the data as "2019-07-02T06:05:00". How should I change the code so that the extracted data looks like "2019-07-02 06:05:00"? I would be grateful for some help!

moraisrn
  • 50
  • 6
  • Please add what you have tried before posting. This is a one line statement with the `str_replace` function from the `stringr` package. `str_replace(date, "T", " ")` – mattyx17 Jul 20 '20 at 17:08
  • Welcome to SO. [POSIXct_xt help](https://stat.ethz.ch/R-manual/R-devel/library/base/html/DateTimeClasses.html) says, Using c on "POSIXlt" objects converts them to the current time zone, and on "POSIXct" objects drops any "tzone" attributes (even if they are all marked with the same time zone), which is what your T is, so `my_date_time_without_T <- c(my_POSIXct)`, HTH – Chris Jul 20 '20 at 17:12

1 Answers1

0

It looks like all data will have the same format, in which case you can use gsub function to replace all character T with a space. I believe you can find more information here: Replace specific characters within strings

> test.data <- c("2019-07-02T05:05:00")
> test.data
[1] "2019-07-02T05:05:00"
> gsub("T", " ", test.data)
[1] "2019-07-02 05:05:00"
nilesguo
  • 162
  • 1
  • 14
  • Thanks. I added that line in my code and it worked perfectly. My code: date<-as.vector(unlist(data$.attrs[5])) date <- gsub("T", " ", date)df$date <- as.POSIXct(date, format = "%Y-%m-%d %H:%M:%S", tz = 'Etc/GMT+5') – moraisrn Jul 20 '20 at 18:05