r datetime conversion from chr using strptime gives NA - r

I have a data frame containing date time information as characters in the format dd/mm/yyyy hh:mm but I can't get it to convert e.g
$ LaserStart : chr "07/12/2014 11:21" "13/12/2014 05:37"
I am trying to convert them to date time using
data.LotCT$Start <- strptime(data.LotCT$LaserStart, "%d/%B/%Y %H:%M")
this runs without producing any errors but when I review the dataframe I have only NA
$ Start : POSIXlt, format: NA NA NA ...
thanks in advance

> x <- "07/12/2014 11:21"
> y <- strptime(x, format='%m/%d/%Y %H:%M')
> strftime(y, '%d/%B/%Y %H:%M')
[1] "12/July/2014 11:21"

Just figured it out
data.LotCT$Start <- strptime(data.LotCT$LaserStart, "%d/%B/%Y %H:%M")
should be
data.LotCT$Start <- strptime(data.LotCT$LaserStart, "%d/%m/%Y %H:%M")
which gives
$ Start : POSIXlt, format: "2014-12-07 11:21:00" "2014-12-13 05:37:00"
sorry for bothering you all :)

Related

How to add a time (midnight) to a date in R and print it without a 1 or 2h shift?

I have a data frame with a Date columns, without time. I would like to convert it to a date time format, using 00:00:00 as time stamp. And print the time as well.
From these posts 1, 2 and 3, I get that time formatting in R might omit midnight, so I then use #ACuriousCat solution to print the time. The simpler code I have is:
data<-c(NA,"2014-03-18","2014-04-01","2014-04-15","2014-04-28","2014-05-14")
> data
[1] NA "2014-03-18" "2014-04-01" "2014-04-15" "2014-04-28" "2014-05-14"
> data1<-format(as.POSIXct(data,tz='UTC'),"%Y-%m-%d %H:%M:%S")
> data1
[1] NA "2014-03-18 00:00:00" "2014-04-01 00:00:00" "2014-04-15 00:00:00" "2014-04-28 00:00:00"
[6] "2014-05-14 00:00:00"
Which works great! However, on my real dataset, the time will be
> data1
[1] NA "2014-03-18 01:00:00" "2014-04-01 02:00:00" "2014-04-15 02:00:00" "2014-04-28 02:00:00"
[6] "2014-05-14 02:00:00"
It looks like a time zone issue + a daylight saving time issue in the way my data is read or coded in R. But how could I solve that? I tried different time zone, it didn't work. All I can do so far to solve it is:
> data1<-format(as.POSIXct(as_datetime(as.double(as.POSIXct(data)+3600)-3600),tz='UTC'),"%Y-%m-%d %H:%M:%S")
> data1
[1] NA "2014-03-18 00:00:00" "2014-04-01 00:00:00" "2014-04-15 00:00:00" "2014-04-28 00:00:00"
[6] "2014-05-14 00:00:00"
Is there a less convoluted way to code this?
It seems that in your manual check and sample you have your dates as a character string, and where it goes wrong on your real data table / frame you have probably the dates as a Date column (with another TZ set).
Here illustrated with dates (character) and dates2 (as.Date)
data <- data.table(
dates = c(NA,"2014-03-18","2014-04-01","2014-04-15","2014-04-28","2014-05-14")
)
data[, dates2 := as.Date(dates)]
data[, datetime := format(as.POSIXct(dates, tz = "UTC"), "%m-%d-%Y %H:%M:%S")]
data[, datetime2 := format(as.POSIXct(dates2, tz = "UTC"), "%m-%d-%Y %H:%M:%S")]
str(data)
# Classes ‘data.table’ and 'data.frame': 6 obs. of 4 variables:
# $ dates : chr NA "2014-03-18" "2014-04-01" "2014-04-15" ...
# $ dates2 : Date, format: NA "2014-03-18" "2014-04-01" "2014-04-15" ...
# $ datetime : chr NA "03-18-2014 00:00:00" "04-01-2014 00:00:00" "04-15-2014 00:00:00" ...
# $ datetime2: chr NA "03-18-2014 01:00:00" "04-01-2014 02:00:00" "04-15-2014 02:00:00" ...
# - attr(*, ".internal.selfref")=<externalptr>
Edit
If you work with a character column with dates you can use this
data[, dates := as.character(dates)]
data[, datetime := format(as.POSIXct(dates, tz = "UTC"), "%m-%d-%Y %H:%M:%S")]
If you had converted your dates to a Date colum you can use this
data[, dates := as.Date(dates)]
data[, datetime := format(as.POSIXct(dates), "%m-%d-%Y %H:%M:%S", tz = "UTC")]
As format returns a string anyhow, the best solution is actually this:
data[!is.na(dates), datetime := paste(dates, "00:00:00")]

character to date in Rshiny

I have an excel which includes dates. I'm importing this excel file into a 'data frame'.
After importing, I tried to convert one column into an date format, but it's displaying 'NA'
What I tried:
str(df$Date_of_visit) # prints type before conversion
df$Date_of_visit # values in the column
df$Date_of_visit <- as.Date(df$Date_of_visit, origin = "1899-12-30", format="%m%d%y") #converting to date
str(df$Date_of_visit) # prints type after conversion
print(df$Date_of_visit) # values in the column
Output I got :
chr [1:4] "43503" "43319" "43473" "43473"
Date[1:4], format: NA NA NA NA
[1] NA NA NA NA
Can someone help me out? What is the mistake I'm doing here?
Thanks in advance!
Regards
Mouni.
You need to not specify format= argument in your as.Date(), and convert the characters to numeric before using as.Date(). Example:
dte <- c("43503","43319","43473","43473")
dte <- as.Date(as.numeric(dte), origin = "1899-12-30")
dte
#[1] "2019-02-07" "2018-08-07" "2019-01-08" "2019-01-08"
format(dte, "%m%d%Y")
#[1] "02072019" "08072018" "01082019" "01082019"
You can use format() to convert the Date objects to character of your choice of format. Note that format() gives you character object, not Date anymore.

strptime outputing NA when I try and convert from "%Y-%m" format

Strptime outputs NA when I set format to "%Y-%m"
I have tried adding the day as a test and it worked, but whenever I do "%Y-%m" or "%m" i get NA
print(strptime("2007-07", format = "%Y-%m"))
[1] NA
print(strptime("07", format = "%m"))
[1] NA
print(strptime("2007", format = "%Y"))
[1] "2007-07-30 EDT"
Use library zoo. It is useful when you have to deal with dates like that.
require(zoo)
yearmon(c(2017,01))
Then you can manipulate the object yearmon.
as.Date(yearmon(c(2017,01)))
[1] "2017-01-01" "7-01-01"

How to convert character into Date in R?

I have an excel file where dates are in below format.
01-Jan-2020
03-Jun-2015
I need to convert this in Date. I have tried with many converting techniques.I am getting NA every time.
This should do it:
x <- c("01-Jan-2020", "03-Jun-2015")
as.Date(x, format = "%d-%b-%Y")
#> [1] "2020-01-01" "2015-06-03"
# Or with lubridate
lubridate::dmy(x)
#> [1] "2020-01-01" "2015-06-03"
We can confirm x was converted from character to date with:
y <- list(input = x,
lubridate = lubridate::dmy(x),
base = as.Date(x, format = "%d-%b-%Y"))
str(y)
#> List of 3
#> $ input : chr [1:2] "01-Jan-2020" "03-Jun-2015"
#> $ lubridate: Date[1:2], format: "2020-01-01" "2015-06-03"
#> $ base : Date[1:2], format: "2020-01-01" "2015-06-03"
Download and install "Lubridate" library.
wget https://github.com/tidyverse/lubridate/archive/v1.6.0.tar.gz
sudo R CMD INSTALL v1.6.0.tar.gz
It contains a method called, "parse_date_time2()"
You can parse the date in any format.
Ex.
parse_date_time2("21 Jan 2010", orders = "dmy")
Check for reference.
https://www.displayr.com/r-date-conversion/

Convert "Jan.2008" to date variable

How would I convert the following character variables to dates?
strDates <- c("Jan.2008", "Feb.2008")
str(strDates)
chr [1:2] "Jan.2008" "Feb.2008"
dates <- as.Date(strDates, "%b %Y")
str(dates)
Date[1:2], format: NA NA
Any assistance would be greatly appreciated
To form a valid 'date', you also need a day which your data was lacking. So we add one, and we simply use an arbitrary day (here: first of the month):
R> strDates <- c("Jan.2008", "Feb.2008")
R> strptime(paste("01", strDates), "%d %b.%Y")
[1] "2008-01-01" "2008-02-01"
R>
A Date requires a day element as well, so you can add that to the input string with paste:
full.dates <- paste("01", strDates, sep = ".")
Specify the template correctly, including separator tokens:
as.Date(full.dates, "%d.%b.%Y")
[1] "2008-01-01" "2008-02-01"

Resources