converting to hh:mm:ss format in r - r

How would I convert seconds into h/m/s format. I've tried to use seconds_to_period but it only gives the value in seconds. e.g
ID Time
1 345 secs
2 121 secs
3 78 secs
I want this is in HH:MM:SS format how is this done?

We may use hms from hms after converting to period
library(lubridate)
df1$Time <- hms::hms(seconds_to_period(readr::parse_number(df1$Time)))
-output
> df1
ID Time
1 1 00:05:45
2 2 00:02:01
3 3 00:01:18
data
df1 <- structure(list(ID = 1:3, Time = c("345 secs", "121 secs", "78 secs"
)), class = "data.frame", row.names = c(NA, -3L))

Related

r: How to calculate duration in hh:mm:s to a fixed time in hours but overlapping midnight

I have the following hh:mm:ss defined in df$times
times
1 22:55:00
2 01:05:00
3 21:00:00
I want to calculate the duration from 20:00:00 to each of these times. However, some times are after midnight and the duration should in this case be estimated to 20:00:00 the 'following day`
Expected output
times new
1 22:55:00 2.92
2 01:05:00 5.08
3 21:00:00 -1.00
Data
df <- structure(list(times = c("22:55:00", "01:05:00", "21:00:00")), class = "data.frame", row.names = c(NA,
-3L))
We convert to period class with hms, create a condition to check whether the compared time value is greater than the 'times', then add 1 day and subtract or else just subtract
library(dplyr)
library(lubridate)
df %>%
mutate(times1 = hms(times), times2 = hms("20:00:00"),
new = as.numeric(case_when(times1 < times2 ~
(times1 + hms("24:00:00") - times2), TRUE ~ times2- times1))/3600 ) %>%
select(times, new)
# times new
#1 22:55:00 -2.916667
#2 01:05:00 5.083333
#3 21:00:00 -1.000000
library(dplyr)
library(data.table)
mydf <- structure(list(times = c("22:55:00", "01:05:00", "21:00:00")), class = "data.frame", row.names = c(NA,
-3L))
mydf %>%
mutate(
times = as.ITime(times),
difference = case_when (
times > as.ITime("20:00:00") ~ difftime(times, as.ITime("20:00:00")),
TRUE ~ difftime(as.ITime("23:59:59"), as.ITime("19:59:59"))
+ difftime( times, as.ITime("00:00:01"))
)
)

Converting Date to Name

I have date's in a dataframe with corresponding sampling date as presented by the sample dataframe:
Date Temp
2016-06-11 5
2017-08-19 12
2018-01-21 13
2019-04-28 7
The date column is in numeric format currently. I want to convert the numeric month (i.e. 06) into its full name (i.e. June) but am having trouble with the conversion.
I did check the converting dates to names question but was confused by the select DATENAME.
You may simply use months(). Example:
d <- transform(d, date.m=months(v))
d
# date x date.m
# 1 2020-10-01 -1.1390886 October
# 2 2020-11-01 -0.6872151 November
# 3 2020-12-01 1.0632769 December
# 4 2021-01-01 1.7351265 January
Note: If your date is not of class "date" you also need to wrap as.Date:
d <- transform(d, date.m=months(as.Date(v)))
Data:
d <- structure(list(date = structure(c(18536, 18567, 18597, 18628), class = "Date"),
x = c(-1.13908860117162, -0.687215137639502, 1.06327693201579,
1.73512650928455)), class = "data.frame", row.names = c(NA,
-4L))

How can I add 1 to a column in R when A conditional is met?

I am trying to fill a new column in a data frame (in R) based on the following conditional:
df$B<- ifelse(difftime(df$A,lag(df$A))>minutes(30), increment(1), increment(0))
Here, the A column is time. So in A, every time the time difference between row i and row i-1 is greater than 30 minutes, I increment the new column B by one.
A B
1:00 1
1:31 2
1:40 2
2:30 3
Example
Any help is greatly appreciated, thank you.
In base R, you can use cumsum with difftime :
df$B <- cumsum(c(TRUE, difftime(df$A[-1], df$A[-nrow(df)], units = 'mins') > 30))
df
# A B
#1 2020-02-03 01:00:00 1
#2 2020-02-03 02:00:00 2
#3 2020-02-03 02:15:00 2
#4 2020-02-03 03:00:00 3
data
Make sure class(df$A) returns "POSIXct" :
df <- structure(list(A = structure(c(1580691600, 1580695200, 1580696100,
1580698800), class = c("POSIXct", "POSIXt"), tzone = "UTC")),
class = "data.frame", row.names = c(NA, -4L))

Harmonizing dates

I have a data frame with dates and the time in it.
Now I want to convert each date into the correct month. How can I do this?
Now it looks like this:
1 01.01.2019 00:00:20.747000
2 21.04.2019 00:00:21.362000
3 31.08.2019 00:00:21.422000
I need it in a format like this:
1 01.01.2019
2 21.04.2019
3 31.08.2019
or eventually like this:
1 January
2 April
3 August
With base R, you can do the following.
First, I wasn't sure if initial data frame was in POSIXct format. I converted it for my example.
Then you can use format to extract the month number or month name.
lubridate is a great package to use for various date manipulations as well and has month function.
df$datetime <- as.POSIXct(df$datetime, format = "%d.%m.%Y %H:%M:%OS")
df$date_only <- as.Date(df$datetime)
df$month_num <- format(df$datetime, "%m")
df$month <- format(df$datetime, "%B")
df
Output
datetime date_only month_num month
1 2019-01-01 00:00:20 2019-01-01 01 January
2 2019-04-21 00:00:21 2019-04-21 04 April
3 2019-08-31 00:00:21 2019-08-31 08 August
Data
df <- structure(list(datetime = c("01.01.2019 00:00:20.747000", "21.04.2019 00:00:21.362000",
"31.08.2019 00:00:21.422000")), class = "data.frame", row.names = c(NA,
-3L))
Try:
df$date <- lubridate::dmy_hms(df$date)
df$date <- format(df$date, "%d.%m.%Y")
data:
df: structure(list(date = c("01.01.2019", "21.04.2019", "31.08.2019"
)), row.names = c(NA, -3L), class = "data.frame")

convert quarter year to last date of quarter in R

I have an issue when I use as.Date(as.yearqtr(test[,1],format ="%qQ%Y"),frac =1), but it returns an error,and quater-year didn't change to date. The error is:
error in as.yearqtr(as.numeric(x)) (list) object cannot be coerced to type 'double'
This is my dataframe in R.
TIME VALUE
1Q2019 1
2Q2019 2
3Q2019 3
4Q2019 4
The ideal output is
TIME VALUE
2019-03-31 1
2019-06-30 2
2019-09-30 3
2019-12-31 4
We can convert to Date with zoo and get the last date of the quarter with frac. We use some RegEx to rearrange in zoo's suitable format:
df$TIME=as.Date(as.yearqtr(gsub("(\\d)(Q)(\\d{1,})","\\3 Q\\1",df$TIME)),frac = 1)
df
TIME VALUE
1 2019-03-31 1
2 2019-06-30 2
3 2019-09-30 3
4 2019-12-31 4
Data:
df <-structure(list(TIME = structure(1:4, .Label = c("1Q2019", "2Q2019",
"3Q2019", "4Q2019"), class = "factor"), VALUE = 1:4), class = "data.frame", row.names = c(NA,
-4L))
Here is a function that will return a vector of dates, given an input vector in the form of 1Q2019...
dateStrings <- c("1Q2019","2Q2019","3Q2019","4Q2019","1Q2020")
lastDayOfQuarter <- function(x){
require(lubridate)
result <- NULL
months <-c(3,6,9,12)
days <- c(31,30,30,31)
for(i in 1:length(x)) {
qtr <- as.numeric(substr(x[i],1,1))
result[i] <- mdy(paste(months[qtr],days[qtr],(substr(x[i],3,6)),sep="-"))
}
as.Date(result)
}
lastDayOfQuarter(dateStrings)
and the output:
>lastDayOfQuarter(dateStrings)
[1] "2019-03-31" "2019-06-30" "2019-09-30" "2019-12-31" "2020-03-31"
>

Resources