R: Lubridate outputformat DMY - r

I was studying the cheat-sheet and other ressources, however I always found examples using lubridatefrom tidyverseputting me the result date into the format Y-M-D
For example having in the raw data the following:
01.06.2016 whhich says First July of 2016 and running:
Date.last.Contact = lubridate::dmy(Date.last.Contact) giving me 2016-06-01
I just want the german format like 01-06-2016. I have to mutate it into date-format to calculate the minimal date od some datarows in my r-script.
I was looking for a parameter where I can specify the output format, however I did not find the hint.

You want
format(ymd(20200102), "%d.%m.%Y")
output:
"02.01.2020"
It is simply about the way you format the output string that is create from the date vector.

Related

Possible to have date variable in day-month-year format in R?

I'm making a 'Make your own graph tutorial' using Rmarkdown,
I have a dataset with a date variable (format: day-month-year), using dmy(variable) I can correctly class the variable as a 'Date' variable (format:year-month-day).
Users have to filter based on this date, and seeing as Dutch people use the day-month-year format I want to allow them to filter based on this format. (I could simply tell them to filter in the year-month-day format but this complicates the exercise.)
Goal of question: getting R to recognize a variable as date, but using a day-month-year format
date <- "28-02-2020"
date <- as.Date(date, "%d-%m-%Y")
class(date)
again: date now gives "2020-02-28" - I want "28-02-2020" (with class: Date)
As far as I understand, no, it is not possible.
Internally, R stores dates as the number of days since January 1, 1970, and prints them out in format "YYYY-mm-dd". Given that people use different formats around the world, it would be really messy if it would be otherwise.
As a side note, if you need to plot your data, ggplot2 gives you the option to properly format your dates, without having to transform them into characters or factors.
As a side note 2, base r lets you adjust the format of the date, and this depends on the system locale that are available, which you can set (this is different on different systems, I am using ubuntu):
Sys.setlocale("LC_TIME", "en_US.utf8")
format(Sys.Date(), format = "%Y-%b-%d")
[1] "2020-May-01"
Sys.setlocale("LC_TIME", "de_CH.utf8")
format(Sys.Date(), format = "%Y-%b-%d")
[1] "2020-Mai-01"
See more info here:
How to change the locale of R?
How to set the default language of date in R

How do I rearrange dates in R?

Here is just one date that I have (out of more than 6,000)
02/01/15
This is expressed as January 2nd, 2015. And I would like the date to instead look like the following,
2015/01/02
I read up on this thread: Changing date format to "%d/%m/%Y"
But unless my R does not work properly, none of the answers give the correct format, instead I get this output,
0002/01/15
You can do:
format(as.Date("02/01/15", format = "%d/%m/%y"), "%Y/%m/%d")
[1] "2015/01/02"
The lubridate package is very useful for date and time manipulation
library(lubridate)
x <- lubridate::dmy('02/01/15')
format(x, format = ('%Y/%m/%d'))

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:
I
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.

How to convert date and time into a numeric value in R

I am relatively new to R and I have a dataset in which I am trying to convert a date and time into a numeric value. The date and time are in the format 01JUN17:00:00:00 under a variable called pickup_datetime. I have tried using the code
cab_small_sample$pickup_datetime <- as.numeric(as.Date(cab_small_sample$pickup_datetime, format = '%d%b%y'))
but this way doesn't incorporate time, I tried to add the time format to the format section of code but still did not work. Is there an R function that will convert the data into a numeric value>
R has two main time classes: "Date" and "POSIXct". POSIXct is a datetime class and you can get all the gory details at: ? DateTimeClasses. The help page for the formats used at the time of data input, however, are at ?striptime.
cab_small_sample <- data.frame(pickup_datetime = "01JUN17:00:00:00")
cab_small_sample$pickup_dt <- as.numeric(as.POSIXct(cab_small_sample$pickup_datetime,
format = '%d%b%y:%H:%M:%S'))
cab_small_sample
# pickup_datetime pickup_dt
#1 01JUN17:00:00:00 1496300400 # seconds since 1970-01-01
I find that a "destructive reassignment of values" is generally a bad idea so as a "my (best?) practice rule" I don't assign to the same column until I'm sure I have the code working properly. (And I always leave an untouched copy somewhere safe.)
lubridate is an extremely handy package for dealing with dates. It includes a variety of functions which do the date/time parsing for you, as long as you can provide the order of components. In this case, since your data is in day-month-year-hms form, you can use the dmy_hms function.
library(lubridate)
cab_small_sample <- dplyr::tibble(
pickup_datetime = c("01JUN17:00:00:00", "01JUN17:11:00:00"))
cab_small_sample$pickup_POSIX <- dmy_hms(cab_small_sample$pickup_datetime)

Converting integer format date to double format of date

I have date format in following format in a data frame:
Jan-85
Apr-99
1-Nov
Feb-96
When I see the typeof(df$col) I get the answer as "integer".
Actually when I see the format in excel it is in m/d/yyyy format. I was trying to convert this to date format in R. All my efforts yielded NA.
I tried parse_date_time function. I tried as.date along with as.character. I tried as.POSIXct but everything is giving me NA.
My trials were as follows and everything was a failure:
as.Date.numeric(df$col,"m%d%Y")
transform(df$col, as.Date(as.character(df$col), "%m%d%Y"))
as.Date(df$col,"m%d%Y")
as.POSIXct.numeric(as.character(loan_new$issue_d), format="%Y%m%d")
as.POSIXct.date(as.character(df$col), format="%Y%m%d")
mdy(df$col)
parse_date_time(df$col,c("mdy"))
How can I convert this to date format? I have used lubridate package for parse_date_time and mdy package.
dput output is below
Label <- factor(c("Apr-08",
"Apr-09", "Apr-10", "Apr-11", "Aug-07", "Aug-08", "Aug-09", "Aug-10",
"Aug-11", "Dec-07", "Dec-08", "Dec-09", "Dec-10", "Dec-11", "Feb-08",
"Feb-09", "Feb-10", "Feb-11", "Jan-08", "Jan-09", "Jan-10", "Jan-11",
"Jul-07", "Jul-08", "Jul-09", "Jul-10", "Jul-11", "Jun-07", "Jun-08",
"Jun-09", "Jun-10", "Jun-11", "Mar-08", "Mar-09", "Mar-10", "Mar-11",
"May-08", "May-09", "May-10", "May-11", "Nov-07", "Nov-08", "Nov-09",
"Nov-10", "Nov-11", "Oct-07", "Oct-08", "Oct-09", "Oct-10", "Oct-11",
"Sep-07", "Sep-08", "Sep-09", "Sep-10", "Sep-11"))
NA is typically what you get when you misspecify the format. Which is what you do. That said, if your data is really looking like the first example you gave, it's impossible to simply convert this to a date. You have two different formats, one being month-year and the other day-month.
If your updated date (i.e. Dec-11) is the correct format, then you use the format argument of as.Date like this:
date <- "Dec-11"
as.Date(date, format = "%b-%d")
# [1] "2017-12-11"
Or on your example data:
as.Date(Label, format = "%b-%d")
# [1] "2017-04-08" "2017-04-09" "2017-04-10" "2017-04-11" "2017-08-07" "2017-08-08"
# [7] "2017-08-09" "2017-08-10" "2017-08-11" "2017-12-07" "2017-12-08" "2017-12-09"
If you want to convert something like Jan-85, you have to decide which day of the month that date should have. Say we just take the first of each month, then you can do:
x <- "Jan-85"
xd <- paste0("1-",x)
as.Date(xd, "%d-%b-%y")
# [1] "1985-01-01"
More information on the format codes can be found on ?strptime
Note that R will automatically add this year as the year. It has to, otherwise it can't specify the date. In case you do not have a day of the month (eg like Jan-85), conversion to a date is impossible because the underlying POSIX algorithms don't have all necessary information.
Also keep in mind that this only works when your locale is set to english. Otherwise you have a big chance your OS won't recognize the month abbreviations correctly. To do so, do eg:
Sys.setlocale(category = "LC_TIME", locale = "English_United Kingdom")
You can later set it back to the original one if you must, or restart your R session to reset the locale settings.
note: Please check carefully which locale notations are valid for your OS. The above example works on Windows, but is not guaranteed on either Linux or Mac.
Why you see integer
The fact that these string values are of integer type, is due to the fact that R automatically convert character vectors to factors when reading in a data frame. So typeof() returns integer because that's the internal representation of a factor.

Resources