I have imported some data into R, which looks like the following:
dateTime temp
1 10/25/2005 12:00:00 15.50
2 10/25/2005 1:00:00 15.49
3 10/25/2005 2:00:00 15.52
4 10/25/2005 3:00:00 15.50
5 10/25/2005 4:00:00 15.50
6 10/25/2005 5:00:00 15.46
where the class of the dateTime column of the data.frame is factor and the second column is numeric.
I try to convert the dateTime into POSIXct format as follows:
dat[,1] <- as.POSIXct(dat[,1])
but receive the error
Error in as.POSIXlt.character(as.character(x), ...) :
character string is not in a standard unambiguous format
which I think is to do with the dateTime varying in the format that hour is presented e.g. 12, 1, 2 etc and not 12, 01, 02.
How can I change this to POSIXct?
You need to specify the format:
datetime <- factor("10/25/2005 12:00:00")
as.POSIXct(datetime)
#Error in as.POSIXlt.character(as.character(x), ...) :
# character string is not in a standard unambiguous format
as.POSIXct(datetime, format="%m/%d/%Y %H:%M:%S")
#[1] "2005-10-25 12:00:00 CEST"
Note: I advise you to always specify a time zone explicitly when creating datetime variables. Otherwise, you could get into trouble with daylight saving time.
as.POSIXct(datetime, format="%m/%d/%Y %H:%M:%S", tz="GMT")
#[1] "2005-10-25 12:00:00 GMT"
Related
I currently have a dataset with multiple different time formats(AM/PM, numeric, 24hr format) and I'm trying to turn them all into 24hr format. Is there a way to standardize mixed format columns?
Current sample data
time
12:30 PM
03:00 PM
0.961469907
0.913622685
0.911423611
09:10 AM
18:00
Desired output
new_time
12:30:00
15:00:00
23:04:31
21:55:37
21:52:27
09:10:00
18:00:00
I know how to do them all individually(an example below), but is there a way to do it all in one go because I have a large amount of data and can't go line by line?
#for numeric time
> library(chron)
> x <- c(0.961469907, 0.913622685, 0.911423611)
> times(x)
[1] 23:04:31 21:55:37 21:52:27
The decimal times are a pain but we can parse them first, feed them back as a character then use lubridate's parse_date_time to do them all at once
library(tidyverse)
library(chron)
# Create reproducible dataframe
df <-
tibble::tibble(
time = c(
"12:30 PM",
"03:00 PM",
0.961469907,
0.913622685,
0.911423611,
"09:10 AM",
"18:00")
)
# Parse times
df <-
df %>%
dplyr::mutate(
time_chron = chron::times(as.numeric(time)),
time_chron = if_else(
is.na(time_chron),
time,
as.character(time_chron)),
time_clean = lubridate::parse_date_time(
x = time_chron,
orders = c(
"%I:%M %p", # HH:MM AM/PM 12 hour format
"%H:%M:%S", # HH:MM:SS 24 hour format
"%H:%M")), # HH:MM 24 hour format
time_clean = hms::as_hms(time_clean)) %>%
select(-time_chron)
Which gives us
> df
# A tibble: 7 × 2
time time_clean
<chr> <time>
1 12:30 PM 12:30:00
2 03:00 PM 15:00:00
3 0.961469907 23:04:31
4 0.913622685 21:55:37
5 0.911423611 21:52:27
6 09:10 AM 09:10:00
7 18:00 18:00:00
I have a column of dates in an R data frame, that look like this,
Date
2020-08-05
2020-08-05
2020-08-05
2020-08-07
2020-08-08
2020-08-08
So the dates are formatted as 'yyyy-mm-dd'.
I am writing this data frame to a CSV that needs to be formatted in a very specific manner. I need to convert these dates to the format 'mm/dd/yyyy hh:mm:ss', so this is what I want the columns to look like:
Date
8/5/2020 12:00:00 AM
8/5/2020 12:00:00 AM
8/5/2020 12:00:00 AM
8/7/2020 12:00:00 AM
8/8/2020 12:00:00 AM
8/8/2020 12:00:00 AM
The dates do not have a timestamp attached to begin with, so all dates will need a midnight timestamp in the format shown above.
I spent quite some time trying to coerce this format yesterday and was unable. I am easily able to change 2020-08-05 to 8/5/2020 using as.Date(), but the issue arises when I attempt to add the midnight time stamp.
How can I add a midnight timestamp to these reformatted dates?
Thanks so much for any help!
You can use format:
df <- data.frame(Date = as.Date(c("2020-08-05", "2020-08-07")))
format(df$Date, "%d-%m-%Y 12:00:00 AM")
[1] "05-08-2020 12:00:00 AM" "07-08-2020 12:00:00 AM"
dat <- data.frame(
Date = as.Date("2020-08-05") + c(0, 0, 0, 2, 3, 3)
)
dat[["Date"]] <- format(dat[["Date"]], "%m/%d/%Y %I:%M:%S %p")
dat[["Date"]] <- sub("([ap]m)$", "\\U\\1", dat[["Date"]], perl = T)
dat
## Date
## 1 08/05/2020 12:00:00 AM
## 2 08/05/2020 12:00:00 AM
## 3 08/05/2020 12:00:00 AM
## 4 08/07/2020 12:00:00 AM
## 5 08/08/2020 12:00:00 AM
## 6 08/08/2020 12:00:00 AM
Try this:
format(as.POSIXct("2022-11-08", tz = "Australia/Sydney"), "%Y-%m-%d %H:%M:%S")
I am new at using R and I am encountering a problem with historical hourly electric load data that I have downloaded.My goal is to make a load forecast based on an ARIMA model and/or Artificial Neural Networks.
The problem is that the data is in the following Date-time (hourly) format:
#> DateTime Day_ahead_Load Actual_Load
#> [1,] "01.01.2015 00:00 - 01.01.2015 01:00" "6552" "6100"
#> [2,] "01.01.2015 01:00 - 01.01.2015 02:00" "6140" "5713"
#> [3,] "01.01.2015 02:00 - 01.01.2015 03:00" "5950" "5553"
I have tried to make a POSIXct object but it didn't work:
as.Date.POSIXct(DateTime, format = "%d-%m-%Y %H:%M:%S", tz="EET", usetz=TRUE)
The message I get is that it is not in an unambiguous format. I would really appreciate your feedback on this.
Thank you in advance.
Best Regards,
Iro
You have 2 major problems. First, your DateTime column contains two dates, so you need to split that column into two. Second, your format argument has - characters but your date has . characters.
We can use separate from tidyr and mutate with across to change the columns to POSIXct.
library(dplyr)
library(tidyr)
data %>%
separate(DateTime, c("StartDateTime","EndDateTime"), " - ") %>%
mutate(across(c("StartDateTime","EndDateTime"),
~ as.POSIXct(., format = "%d.%m.%Y %H:%M",
tz="EET", usetz=TRUE)))
StartDateTime EndDateTime Day_ahead_Load Actual_Load
1 2015-01-01 00:00:00 2015-01-01 01:00:00 6552 6100
2 2015-01-01 01:00:00 2015-01-01 02:00:00 6140 5713
3 2015-01-01 02:00:00 2015-01-01 03:00:00 5950 5553
i have this table of consumptions. I am trying to convert the first two columns into a one xts date format.
1 01.01.2016 00:00:00 26.27724
2 01.01.2016 01:00:00 24.99182
3 01.01.2016 02:00:00 23.53261
4 01.01.2016 03:00:00 22.46478
5 01.01.2016 04:00:00 22.00291
6 01.01.2016 05:00:00 21.95708
7 01.01.2016 06:00:00 22.20354
8 01.01.2016 07:00:00 21.84416
i have tried the code belo and got that error.
timestamp=format(as.POSIXct(paste(datecol,hourcol)), "%d/%m/%Y %H:%M:%S")
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
the date is character and hour is in double format.
If you were trying to combine date and time value to create timestamp, we can use as.POSIXct in base R.
df$timestamp <- as.POSIXct(paste(df$datecol,df$hourcol),
format = "%d.%m.%Y %T", tz = "UTC")
Or using lubridate
df$timestamp <- lubridate::dmy_hms(paste(df$datecol,df$hourcol))
Or using anytime
df$timestamp <- anytime::anytime(paste(df$datecol,df$hourcol))
I tried to import csv with date format:
3/1/2017 0:00
3/1/2017 1:00
3/1/2017 2:00
3/1/2017 3:00
3/1/2017 4:00
3/1/2017 5:00
into R, however the date format appears in R become:
2017-03-01 00:00:00 2017-03-01 01:00:00 2017-03-01 02:00:00 2017-03-01 03:00:00 2017-03-01 04:00:00 2017-03-01 05:00:00
How can I read csv into R as the original format without changing anything?
It is in the "original" format, in the sense that you're probably looking at a POSIXct or POSIXlt object. You can reformat dates and datetimes using format() or strftime(), but this will render them character.
So as long as you're working with the datetime objects, just leave it as is. If you need to report, you can use any of the aforementioned functions to format the string:
x <- "3/1/2017 3:00"
x1 <- as.POSIXct(x, format = "%d/%m/%Y %H:%M")
x1
# [1] "2017-01-03 03:00:00 CET"
strftime(x1, format = "%d/%m/%Y %H:%M")
# [1] "03/01/2017 03:00"
format(x1, format = "%d/%m/%Y %H:%M")
# [1] "03/01/2017 03:00"