Search code examples
rtimestampmongolite

converting character from mongolite to timestamp in R


I have a question. I am downloading some data from mongodb and then I want to do sam calculations of this data. Unfortunately I get timestamp as a string I and don't know how to convert it back to timestamp.

MaxDate <- con_string$find(query = '{}', sort = '{"timestamp":-1}', limit = 1)$timestamp

Above code returns to me maximum date from column timestamp. But format of that is for me totally useful.

"Aug 14 2019  8:57AM"

Any ideas how to convert it to interpretable by R version of timestamp?

Update:

Ienter image description here


Solution

  • Here is a good link on how to modify strings to dates: https://stats.idre.ucla.edu/r/faq/how-can-i-format-a-string-containing-a-date-into-r-date-object/

    It has multiple formats you might want to compare with. For your specific example, I think this should work:

    MaxDate <- as.Date(MaxDate, "%b %d %Y")
    

    if you want to save the Date part only. If you also want to use the time, there is another method you could use for that:

    strptime(temp, format="%b %d %Y %H:%M%p")
    

    More information about as.Date() and formats you can find here: as.Date() helper

    More information about strptime (date + time) you can find here: striptime helper

    UPDATE: I found that package in R that might be helpful for you to avoid multiple conversions: timestamp conversions You can cast the timestamp data to the measurable time stamp.