converting mddyy date to date - r

I have a date column in a .csv spreadsheet generated in inquisit.
the format is mddyy, so the 13th of July is 71315. R recognizes this as an integer.
Can anyone recommend a way to convert this to ISO 8601 date format?

Since you mention that the day is always two numbers, you could use a little sprintf() magic to add zeros out front.
as.Date(sprintf("%06d", 71315), "%m%d%y")
# [1] "2015-07-13"
The sprintf() call here adds zeros up to 6 characters and also turn it into a character vector, so as.Date() will accept it.
sprintf("%06d", 71315)
# [1] "071315"

We could also use mdy from library(lubridate)
library(lubridate)
mdy(71315) #returns POSIXct class.
#[1] "2015-07-13 UTC"
as.Date(mdy(71315)) #convert to `Date` class.
#[1] "2015-07-13"

Related

as.Date returns the day with four digits in R

I am trying to convert a character to date format, but "day" is replaced with 4 digits.
> mydate="20/3/20"
> mydate=as.Date(mydate)
> mydate
[1] "0020-03-20"
This didn't happen a few days ago, only now as I re-run an old script. I don't know if this has to do with me upgrading recently to R version 3.6.3.
I tried to remove the "00" with gsub, but it turns the date back to character again.
> mydate=gsub("00","",mydate)
> class(mydate)
[1] "character"
Does anyone know how I can fix that with mydate remaining as date, or why this happens?
Thank you in advance
The default format for Date class is "%Y-%m-%d" i.e. 4 digit year followed by a dash, followed by two digits, then a dash and two digits. Any other format, we need to specify the format argument in as.Date
as.Date(mydate, "%d/%m/%y")
#[1] "2020-03-20"
We need to be very careful with two-digit year as it can lead some strange output. It is better to have 4 digit year

Convert time (24 hours format) in character type to time in R

I have a data frame with time in character format, I need to convert it into time format. I have tried using strptime and POSIXct but it adds the date also. I just need the time.
For e.g.: TRK_DEP_TIME <- c("22:00", "14:30"......) _____ character datatype
doing........ as.POSIXCT(TRK_DEP_TIME, format = %H:%M")
The result will be ("10/11/17,22:00", "10/11/17, 14:30".....)
I am looking for just the time, I don't need the date to be associated with it. Is there any way I can achieve this?
Use chron "times" class:
library(chron)
ch <- c("22:00", "14:30") # test input (character)
times(paste0(ch, ":00"))
## [1] 22:00:00 14:30:00

Converting timestamp in seconds to a date format in R

I have a table (tags) with a column for timestamp (ts), which is formatted as seconds since 1 Jan, 1970 GMT. I'm trying to create a date column that converts the timestamp from seconds to date and time EST.
The suggested code for R was:
tags$date<-strptime(tags$ts, "%Y-%m-%d")
tags$date<-as.POSIXct(tags$date)
But when I do this, tags$date comes up as NA. Any suggestions for what I might be doing wrong? Thanks.
You should us as.POSIXct function instead:
tags$date <- as.POSIXct(tags$ts, origin="1970-01-01", tz="US/New York")
strptime converts between character representations and dates not between timestamp and dates.
Here's a lubridate version. When we use as_datetime we don't need to explicitly specify an origin as it defaults to the desired origin.
lubridate::as_datetime(1507119276, tz='EST')
# [1] "2017-10-04 07:14:36 EST"

Formatting Unconventional Date

I'm having trouble formatting a list of dates in R. The conventional methods of formatting in R such as as.Date or as.POSIXct don't seem to be working.
I have dates in the format: 1012015
using
as.POSIXct(as.character(data$Start_Date), format = "%m%d%Y")
does not give me an error, but my date returns
"0015-10-12" because the month is not a two digit number.
Is there a way to change this into the correct date format?F
The lubridate package can help with this:
lubridate::mdy(1012015)
[1] "2015-01-01"
The format looks ambiguous but the OP gave two hints:
He is using format = "%m%d%Y" in his own attempt, and
he argues the issue is because the month is not a two digit number
This uses only base R. The %08d specifies a number to be formatted into 8 characters with 0 fill giving in this case "01012015".
as.POSIXct(sprintf("%08d", 1012015), format = "%m%d%Y")
## [1] "2015-01-01 EST"
Note that if you don't have any hours/minutes/seconds it would be less error prone to use "Date" class since then the possibility of subtle time zone errors is eliminated.
as.Date(sprintf("%08d", 1012015), format = "%m%d%Y")
## [1] "2015-01-01"

Convert dates and times string to numeric

I have a file with nearly four thousand entries in a column formatted like this:
1/28/2015 14:13
How do I get R to read these as real numbers?
As #RomanLuštrik suggested:
mydate <- "1/28/2015 14:13"
# convert to date
strptime(mydate, "%m/%d/%Y %H:%M")
# [1] "2015-01-28 14:13:00 GMT"
# make it numeric
as.numeric(strptime(mydate, "%m/%d/%Y %H:%M"))
# [1] 1422454380
datestring<-"your variable"
x<-strptime(datestring, %b/%d,%Y %H:%M)
Just check out the strptime() info
there is the lubridate package with a lot of functions for this for changing formats
for real numbers you have POSIXct() function.

Resources