Dates and POSIXct - r

I read a file with the function
site_wind <- read.delim(import,header=F,sep="\t",skip=nline,quote="\"")
In the first column I have dates and times in the form:
01/05/2011 0:10 where "day-month-year hour:min"
I want to convert site_wind$V1 to class POSIXct and POSIXlt but when I do it:
as.POSIXct(site_wind$V1,"%d-%m-%Y %H:%M",TZ="GMT")
and I get:
"0026-01-20 GMT"
I have tried some alternatives, but I don't know how to solve this problem.

You need literal / as the delimiter in the dates. In the format = "%d-%m-%Y %H:%M" part you are using literal - as the data separator, which doesn't match the date example you showed. I think you want
as.POSIXct(as.character(site_wind$V1), format = "%d/%m/%Y %H:%M", tz="GMT")
Note that the argument is tz not TZ - R was silently ignoring that in your original call.

Related

Formatting Dates in R with times in the same column [duplicate]

I have a data frame with a character column of date-times.
When I use as.Date, most of my strings are parsed correctly, except for a few instances. The example below will hopefully show you what is going on.
# my attempt to parse the string to Date -- uses the stringr package
prods.all$Date2 <- as.Date(str_sub(prods.all$Date, 1,
str_locate(prods.all$Date, " ")[1]-1),
"%m/%d/%Y")
# grab two rows to highlight my issue
temp <- prods.all[c(1925:1926), c(1,8)]
temp
# Date Date2
# 1925 10/9/2009 0:00:00 2009-10-09
# 1926 10/15/2009 0:00:00 0200-10-15
As you can see, the year of some of the dates is inaccurate. The pattern seems to occur when the day is double digit.
Any help you can provide will be greatly appreciated.
The easiest way is to use lubridate:
library(lubridate)
prods.all$Date2 <- mdy(prods.all$Date2)
This function automatically returns objects of class POSIXct and will work with either factors or characters.
You may be overcomplicating things, is there any reason you need the stringr package? You can use as.Date and its format argument to specify the input format of your string.
df <- data.frame(Date = c("10/9/2009 0:00:00", "10/15/2009 0:00:00"))
as.Date(df$Date, format = "%m/%d/%Y %H:%M:%S")
# [1] "2009-10-09" "2009-10-15"
Note the Details section of ?as.Date:
Character strings are processed as far as necessary for the format specified: any trailing characters are ignored
Thus, this also works:
as.Date(df$Date, format = "%m/%d/%Y")
# [1] "2009-10-09" "2009-10-15"
All the conversion specifications that can be used to specify the input format are found in the Details section in ?strptime. Make sure that the order of the conversion specification as well as any separators correspond exactly with the format of your input string.
More generally and if you need the time component as well, use as.POSIXct or strptime:
as.POSIXct(df$Date, "%m/%d/%Y %H:%M:%S")
strptime(df$Date, "%m/%d/%Y %H:%M:%S")
I'm guessing at what your actual data might look at from the partial results you give.
If you don't know the format you could use anytime::anydate, which tries to match to common formats:
library(anytime)
date <- c("01/01/2000 0:00:00", "Jan 1, 2000 0:00:00", "2000-Jan-01 0:00:00")
anydate(date)
[1] "2000-01-01" "2000-01-01" "2000-01-01"
library(lubridate)
if your date format is like this '04/24/2017 05:35:00'then change it like below
prods.all$Date2<-gsub("/","-",prods.all$Date2)
then change the date format
parse_date_time(prods.all$Date2, orders="mdy hms")

Convert Character date/time with am and pm to date/time format

I am attempting to convert a character date time to a date/time format using strptime. The data is in MDY_HM (1/29/20 3:43pm).
My code currently looks like this:
comp_report_tz$Start_Date_Time <- strptime(comp_report_tz$Start_Date_Time, format = "%m/%d/%y %H:%M %p")
The output for each observation simply shows: "< POSIXlt >"
I don't receive any errors when executing.
Make the following changes:
use %I for the hour
use %p for the am/pm.
ensure that the format pattern is in the precise pattern of the data -- it's not in the question
you likely want POSIXct, not POSIXlt
thus we use this format
as.POSIXct("1/29/20 3:43pm", format = "%m/%d/%y %I:%M%p")
## [1] "2020-01-29 15:43:00 EST"

Cannot convert vector of different formats to POSIXct [duplicate]

This question already has answers here:
How to convert variable with mixed date formats to one format?
(3 answers)
Closed 3 years ago.
I have a dataset containing a date in CHAR format that I want to convert to POSIXct. The dates are always in one of five formats, but for this example two will suffice:
test <- c("15/03/19 17:16", "15/03/19,17:16", "15/03/19,17:16")
formats <- c(
"%d/%m/%y,%H:%M",
"%d/%m/%y %H:%M",
"%d/%m/%Y,%H:%M",
"%d/%m/%Y %H:%M",
"%Y%m%d%H%M%S"
)
as.POSIXct(test[1], tz = "GMT", tryFormats = formats) # works
as.POSIXct(test[2:3], tz = "GMT", tryFormats = formats) # works
as.POSIXct(test, tz = "GMT", tryFormats = formats) # fails
Individually, the two dates convert without issue. When the vector (or in my case, the datatable via mutate) is put through as.POSIXct the following error is generated:
Error in as.POSIXct.character(x, tz, ...) : character string is not in a standard unambiguous format
Presumably as.POSIXct is picking only one of the formats in tryFormat and trying to apply that to the whole set, which won't work.
Is my only option here creating a blank column and manually looping through each individual row to populate it?
Example dates:
15/03/19 17:16
15/03/19,17:16
15/03/2019 17:16
15/03/2019,17:16
20190315171600GS
Try lubridate::dmy_hm which works for both. Also your formats needs correction I believe.
formats <- c("%d/%m/%y,%H:%M", "%d/%m/%y %H:%M")
lubridate::dmy_hm(test)
#[1] "2019-03-15 17:16:00 UTC" "2019-03-15 17:16:00 UTC" "2019-03-15 17:16:00 UTC"
Or with anytime
anytime::addFormats(formats)
anytime::anytime(test)
If we need to specify formats explicitly, we may also use parse_date_time
lubridate::parse_date_time(test, formats)

Problems reading CSV column w/fractional seconds in POSIX format

I am reading a CSV file with two columns specifying a date w/time (fractional seconds). The format is like this: 2015-07-13 09:05:52.761, which is originally a factor.
I tried reading the column in using POSIXlt and several variations of this:
time_d$time_started_visit <- as.POSIXlt(time_d$time_started_visit, format="%Y-%m-%d %H:M%:%OS")
All this did was convert the values of the column to NA. I would really like to convert this so I can get the difference in time between the two columns, any suggestions?
You used M% instead of %M
# If you wish to retain the fractional seconds
options(digits.secs = 3)
as.POSIXlt(x, format="%Y-%m-%d %H:%M:%OS")
You had one little error in your code:
Do not use format="%Y-%m-%d %H:M%:%OS but:
format= "%Y-%m-%d %H:%M:%OS"
You changed %M and M%
The whole code is then:
as.POSIXlt(temp$x, format= "%Y-%m-%d %H:%M:%OS")

Convert date-time string to class Date

I have a data frame with a character column of date-times.
When I use as.Date, most of my strings are parsed correctly, except for a few instances. The example below will hopefully show you what is going on.
# my attempt to parse the string to Date -- uses the stringr package
prods.all$Date2 <- as.Date(str_sub(prods.all$Date, 1,
str_locate(prods.all$Date, " ")[1]-1),
"%m/%d/%Y")
# grab two rows to highlight my issue
temp <- prods.all[c(1925:1926), c(1,8)]
temp
# Date Date2
# 1925 10/9/2009 0:00:00 2009-10-09
# 1926 10/15/2009 0:00:00 0200-10-15
As you can see, the year of some of the dates is inaccurate. The pattern seems to occur when the day is double digit.
Any help you can provide will be greatly appreciated.
The easiest way is to use lubridate:
library(lubridate)
prods.all$Date2 <- mdy(prods.all$Date2)
This function automatically returns objects of class POSIXct and will work with either factors or characters.
You may be overcomplicating things, is there any reason you need the stringr package? You can use as.Date and its format argument to specify the input format of your string.
df <- data.frame(Date = c("10/9/2009 0:00:00", "10/15/2009 0:00:00"))
as.Date(df$Date, format = "%m/%d/%Y %H:%M:%S")
# [1] "2009-10-09" "2009-10-15"
Note the Details section of ?as.Date:
Character strings are processed as far as necessary for the format specified: any trailing characters are ignored
Thus, this also works:
as.Date(df$Date, format = "%m/%d/%Y")
# [1] "2009-10-09" "2009-10-15"
All the conversion specifications that can be used to specify the input format are found in the Details section in ?strptime. Make sure that the order of the conversion specification as well as any separators correspond exactly with the format of your input string.
More generally and if you need the time component as well, use as.POSIXct or strptime:
as.POSIXct(df$Date, "%m/%d/%Y %H:%M:%S")
strptime(df$Date, "%m/%d/%Y %H:%M:%S")
I'm guessing at what your actual data might look at from the partial results you give.
If you don't know the format you could use anytime::anydate, which tries to match to common formats:
library(anytime)
date <- c("01/01/2000 0:00:00", "Jan 1, 2000 0:00:00", "2000-Jan-01 0:00:00")
anydate(date)
[1] "2000-01-01" "2000-01-01" "2000-01-01"
library(lubridate)
if your date format is like this '04/24/2017 05:35:00'then change it like below
prods.all$Date2<-gsub("/","-",prods.all$Date2)
then change the date format
parse_date_time(prods.all$Date2, orders="mdy hms")

Resources