Changing as "Factor" to time - r

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"

Related

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))

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")),]

R - converting Character strings into date with only Year/Month

Goal: Plot a time series.
Problem: X-axis data is of course viewed as a character and I'm having trouble converting the character into a date.
new.df <- df %>%
group_by(Month, Year) %>%
summarise(n = n())
new.df <- new.df %>%
unite(Date, Month, Year, sep = "/") %>%
mutate(Total = cumsum(n))
So, I end up with a data frame looking like this:
Date n Group Total
8/2010 1 1 1
9/2010 414 1 415
etc
I'm trying to convert the Date column into a Date format. The column is a character class. So, I tried doing
new.df$Date <- as.Date(New.Patients$Date, %m/%Y)
However, when I do that, it replaces the entire Date column into NA's.
I'm not sure if this is because my single-digit month dates do not have 0's in front or not. I did the unite() function just because I thought it may make it easier, but it might not.
I originally created the Year/Month variable with the lubridate package but I wasn't sure I could incorporate that here. Bonus points if someone can show me how.
I would appreciate any help or guidance. I'm sure it's not that hard I am just having a major brain fart at the moment.
You can try like this:
library(zoo) # for yearmon
new.df$Date <- as.yearmon(New.Patients$Date, format="%m/%Y")
But if you really need it to be as.Date then I guess you have to define day (e.g. 01) as #lukeA has suggested in comment.
My issue, as pointed out by lukeA in the comments, is that the as.Date function requires a day to be somewhere within the character string.
Therefore, just by pasting "01" (or I think virtually any other two-digit combination would work) to the front of each date fixed the issue.

Problems with dplyr and POSIXlt data

I have a problem. I downloaded data and tranformed dates into POSIXlt format
df<-read.csv("007.csv", header=T, sep=";")
df$transaction_date<-strptime(df$transaction_date, "%d.%m.%Y")
df$install_date<-strptime(df$install_date, "%d.%m.%Y")
df$days<- as.numeric(difftime(df$transaction_date,df$install_date, units = "days"))
Data frame is about transaction in one online game. It contains value (its payment), transaction_date, intall_date and ID. I added new column, which showndays after installation. I tried to summarise data using dlyr
df2<-df %>%
group_by(days) %>%
summarise(sum=sum(value))
And I've got an error:
Error: column 'transaction_date' has unsupported type : POSIXlt, POSIXt
How can i Fix it?
UPD. I changed classes of Date columns into Character. It solved problem. But can i use dlyr withouts changing classes in my dataset?
You could use as.POSIXct as recommended in the comments but if the hours, minutes, and seconds don't matter then you should just use as.Date
df <- read.csv("007.csv", header=T, sep=";")
df2 <- df %>%
mutate(
transaction_date = as.Date(transaction_date, "%d.%m.%Y")
,install_date = as.Date(install_date, "%d.%m.%Y")
) %>%
group_by(days = transaction_date - install_date) %>%
summarise(sum=sum(value))
As noted here, this is a "feature" of the tidyverse. They don't want to handle POSIXlt object because it is some kind of list within a vector. However, using as.POSIXct isn't always an option. In my case I really needed the POSIXlt class to handle some uncleaned data. In that case, just go back to good old stable base R. In your case:
df2 <- aggregate(df1$value, by=list(df$days), sum)
One trick I use often is the following:
Convert POSIXt columns (in example below eventDate) to character
Perform dplyr operations you need (in example below we bind rows of two data frames)
Convert back from character to POSIXt not forgetting to set the right format (format) and timezone (tz) as it was before performing step 1.
Example:
# step 1
df1$eventDate <- as.character.POSIXt(df1$eventDate)
df2$eventDate <- as.character.POSIXt(df2$eventDate)
#step 2
merged_df <- bind_rows(df1, df2)
#step 3
merged_df$eventDate <- strptime(merged_df$eventDate, format = "%Y-%m-%d", tz = "UTC")

Resources