Split and reformat Date column in R - r

I have a CSV file that I open using the read.csv() function.
It has 3 columns, Id, Time and Value.
The Time column is formatted like this: 4/12/2016 7:21:00 AM
First
What I want is to split it into Date 4/12/2016 and Time 7:21:00
Second
Convert the Time into 24 hours format instead of AM and PM.
How can this be accomplished?

If you want to split you can use str_split() from the library(stringr)
my_var <- str_split(string = my_df$Time, pattern = " ", n = 2, simplify = TRUE)
my_df$Date <- my_var[, 1] #In this column you'll find the Date
my_df$Time <- my_var[, 2] #In this column you'll find the Time

lubridate is your friend (along with hms package). First convert your Time variable into a datetime object, then use helper functions to parse out the Date and Time. lubridate uses the 24hour format (POSIXIt) when it parses it.
my_df %>%
mutate(Time = mdy_hms(Time),
Date = as_date(Time),
Time = hms::as_hms(Time))

Related

max() function on date format in R

I have a column in my dataset which is formatted as dates. I am aiming to obtain the most recent date by using the max() function. The date column is formatted like (ex. 21.12.2018), and I have used the folowing lines of code:
MD$Date<- as.Date(MD$Date, "%d.%m.%Y")
analysis_date <- max(MD$Date)
Howevere, analysis_date returns the value NA.
Any tips?
Here an exemple using dplyr from tidyverse:
# package
library(dplyr)
# Sample database
MD <- data.frame(
Date = c("21.12.2018", NA, "20.12.2018", "19.12.2018")
)
# Get the most recent date
MD |>
slice_max(
as.Date(Date, "%d.%m.%Y")
)
I think the reason it returns the NA is that your date is in character
you need to convert your dataset from character to date format.
my_dates_update <- as.Date(my_dates) # Convert character to Date
class(my_dates_update) # Check the class of updated dates, it should return date as type
min(my_dates_updated) # Earliest date
max(my_dates_updated) # Latest date

R: Converting a string to useful date and time values

I have a csv file with a column values like "20140929120000" which gives the date and time.
After importing it to R, I want to format this as a date variable while keeping the time part as well(if that is possible).
So, the output should be a date variable '2014-09-29'.
How would I get the time part as a separate column with value "12:00:00"?
how about
install.packages('lubridate')
library(lubridate)
y <- as.numeric(20140929120000)
df %>%
mutate(Date = as.Date(ymd_hms(y), tz= Sys.timezone()),
Time = format(lubridate::ymd_hms(y), "%H:%M:%S")
just change the Y in the mutate to your date column
We can use the POSIXct type here:
val <- "20140929120000"
mask <- "%Y%m%d%H%M%S"
as.POSIXct(strptime(val, mask))
[1] "2014-09-29 12:00:00 UTC"
To see the various components of the timestamp, try:
unclass(strptime(val, mask))

Split given timestamp into year month day period format

I am writing a R code and I have a problem to split Date format into year, month, day, hour format.
I have dataframe named Metadata has a column X_date, contains timestamp like '01.01.2020 00:00:00', '01.01.2020 01:00:00' and so on.
I want to change this x_date column into additional four columns namely year, month, day and hour. I tried this way using some previous answer:
Metadata$Date <- as.Date(Metadata$X_date)
Metadata$Time <- format(as.POSIXct(Metadata$X_date) ,format = "%Y:%m:%d %H")
It gave me this error:
Error in as.POSIXlt.character(as.character(x), ...) :
character string is not in a standard unambiguous format
I am very new to R programming. Please help me. Thnak you for your time.
The format specified should match the order in which all the components of 'Date/Time' occurs in the original vector. Assuming that 'day' is first, followed by 'month', the coversion in 'POSIXct' would be
as.POSIXct(Metadata$X_date, format = "%d.%m.%Y %H:%M:%S")
As we want to create new columns, this can be done with tidyverse
library(tidyverse)
library(lubridate)
Metadata %>%
mutate(X_date = dmy_hms(X_date),
year = year(X_date),
month = month(X_date),
day = day(X_date),
hour = hour(X_date))

Changing as "Factor" to time

How can I transform a value from Factor to time ? I've tried using lubridate package but had no success.
I have a dataframe with a column "time" with 08:00:00 like values. Then used
phsb1 <- phsb %>%
dplyr::mutate(time = lubridate::hm(time))
with resulted in a class with 6 slots
data year month day hour and minutes
Any help to be able to obtain 08:00 like values would be very much appreciated.
Further more information or advice regarding how to handle "time" would be fantastic. I've found a lot about "dates" but almost nothing related to "time".
I think OP wants to convert a column containing data in H:M:S' format toH:M` format in character.
Option #1: Simply get substring containing part of hour and min using sub as:
library(dplyr)
phsb1 <- phsb %>%
mutate(time = sub("(\\d{2}:\\d{2}):\\d{2}","\\1", as.character(time)))
Option #2: Use parse_date_time from lubridate as.
library(lubridate)
library(dplyr)
phsb1 <- phsb %>%
mutate(time = format(parse_date_time(as.character(time), "HMS"), format = "%H:%M"))
#Example
format(parse_date_time("08:05:00", "HMS"), format = "%H:%M")
#"08:05"

objct Timestamp not found when sort_by [duplicate]

I need to sort a data frame by date in R. The dates are all in the form of "dd/mm/yyyy". The dates are in the 3rd column. The column header is V3. I have seen how to sort a data frame by column and I have seen how to convert the string into a date value. I can't combine the two in order to sort the data frame by date.
Assuming your data frame is named d,
d[order(as.Date(d$V3, format="%d/%m/%Y")),]
Read my blog post, Sorting a data frame by the contents of a column, if that doesn't make sense.
Nowadays, it is the most efficient and comfortable to use lubridate and dplyr libraries.
lubridate contains a number of functions that make parsing dates into POSIXct or Date objects easy. Here we use dmy which automatically parses dates in Day, Month, Year formats. Once your data is in a date format, you can sort it with dplyr::arrange (or any other ordering function) as desired:
d$V3 <- lubridate::dmy(d$V3)
dplyr::arrange(d, V3)
In case you want to sort dates with descending order the minus sign doesn't work with Dates.
out <- DF[rev(order(as.Date(DF$end))),]
However you can have the same effect with a general purpose function: rev(). Therefore, you mix rev and order like:
#init data
DF <- data.frame(ID=c('ID3', 'ID2','ID1'), end=c('4/1/09 12:00', '6/1/10 14:20', '1/1/11 11:10')
#change order
out <- DF[rev(order(as.Date(DF$end))),]
Hope it helped.
You can use order() to sort date data.
# Sort date ascending order
d[order(as.Date(d$V3, format = "%d/%m/%Y")),]
# Sort date descending order
d[rev(order(as.Date(d$V3, format = "%d/%m/%y"))),]
Hope this helps,
Link to my quora answer https://qr.ae/TWngCe
Thanks
If you just want to rearrange dates from oldest to newest in r etc. you can always do:
dataframe <- dataframe[nrow(dataframe):1,]
It's saved me exporting in and out from excel just for sort on Yahoo Finance data.
The only way I found to work with hours, through an US format in source (mm-dd-yyyy HH-MM-SS PM/AM)...
df_dataSet$time <- as.POSIXct( df_dataSet$time , format = "%m/%d/%Y %I:%M:%S %p" , tz = "GMT")
class(df_dataSet$time)
df_dataSet <- df_dataSet[do.call(order, df_dataSet), ]
You could also use arrange from the dplyr library.
The following snippet will modify your original date string to a date object, and order by it. This is a good approach, as you store a date as a date, not just a string of characters.
dates <- dates %>%
mutate(date = as.Date(date, "%d/%m/%Y")) %>%
arrange(date)
If you just want to order by the string (usually an inferior option), you can do this:
dates <- dates %>%
arrange(date = as.Date(date, "%d/%m/%Y"))
If you have a dataset named daily_data:
daily_data <- daily_data[order(as.Date(daily_data$date, format="%d/%m/%Y")),]

Resources