Remove Seconds from Time in R

Remove seconds from time in R

No you are giving as.POSIXct a wrong format...

What about using format

datetimes = as.POSIXct(c("2016-04-02 10:33:45 COT", "2016-04-02 22:19:24 COT" ,"2016-04-09 17:47:13 COT", "2016-04-13 16:56:23 COT")    
format(datetimes,format='%Y%m%d %H:%M')

[1] "20160402 10:33" "20160402 22:19" "20160409 17:47" "20160413 16:56"

How to remove seconds before the first full minute

Since you tagged this with lubridate, here is a lubridate/tidyverse solution. (If I understood your question correctly.)

library(tidyverse)
library(lubridate)

df <- tibble::tribble(
~id, ~timestamp,
1, "2017-03-15 10:29:58",
1, "2017-03-15 10:29:59",
1, "2017-03-15 10:30:00",
1, "2017-03-15 10:30:01",
1, "2017-03-15 12:48:00",
1, "2017-03-15 12:48:01",
1, "2017-03-15 12:48:02",
2, "2017-04-01 11:19:59",
2, "2017-03-15 11:20:00"
) %>%
mutate(
timestamp = as_datetime(timestamp),
x = rnorm(n()) # some var you want aggregate
)

If you just want to keep observations that are "whole minute", as you call it, then keep the observations with time stamps that are the same when floored by minute.

df %>%
filter(timestamp == floor_date(timestamp, "minute"))

If you don't have observations matching exactly the floored time stamp, but you want to keep the observations that are closest to the "whole minute", then you can arrange them by timestamp and keep the first one within each minute.

df %>%
arrange(timestamp) %>%
mutate(min = floor_date(timestamp, "minute")) %>%
group_by(min, id) %>%
slice(1) %>%
ungroup()

If you want to aggregate some variable x by minute, say, take the mean, then group by a floored time stamp.

df %>%
mutate(min = floor_date(timestamp, "minute")) %>%
group_by(min, id) %>%
summarize(mean_var = mean(var)) %>%
ungroup()

Change datetime format by removing the seconds

Use format.

format(x[,1], "%d-%m-%Y %H:%M")
# [1] "20-07-2021 12:37" "20-07-2021 12:37" "20-07-2021 12:37" "20-07-2021 12:37"
# [5] "20-07-2021 12:37" "20-07-2021 12:37" "20-07-2021 12:37" "20-07-2021 12:37"
# [9] "20-07-2021 12:37" "20-07-2021 12:37" "20-07-2021 12:37" "20-07-2021 12:37"
#[13] "20-07-2021 12:37" "20-07-2021 12:37" "20-07-2021 12:37" "20-07-2021 12:37"
#[17] "20-07-2021 12:37" "20-07-2021 12:37" "20-07-2021 12:37" "20-07-2021 12:37"

Data:

x <- structure(list(Collectie_DatumTijd = structure(c(1626784620, 
1626784620, 1626784620, 1626784620, 1626784620, 1626784620, 1626784620,
1626784620, 1626784620, 1626784620, 1626784620, 1626784620, 1626784620,
1626784620, 1626784620, 1626784620, 1626784620, 1626784620, 1626784620,
1626784620), tzone = "UTC", class = c("POSIXct", "POSIXt"))), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))

R: Removing hour,min,sec from date

You could do this also with strftime():

strftime(d, format="%Y-%m-%d")
[1] "2019-02-18"

With format= you can basically choose what you want to extract. It works even with just "%Y" in order to extract the year component.

Data:

d <- as.POSIXlt("2019-02-18 00:00:31 IST")

regex remove seconds and milliseconds

You can try this regex, which I added a bit:

gsub("(\\d{4})(\\d{2})(\\d{2}) (\\d{2}:\\d{2}).*", "\\1-\\2-\\3 \\4:00", subject, perl=TRUE);

demo on regex101.

Remove time values from a data frame which are within 10 seconds of each other in R

Here is one dplyr answer -

library(dplyr)

df %>%
mutate(Timestamp = as.POSIXct(Time, format = '%T')) %>%
filter(difftime(Timestamp, lag(Timestamp, default = first(Timestamp) - 11), units = 'sec') > 10) %>%
select(-Timestamp)

# Time ID
#1 07:00:48 00003F9776
#2 11:45:34 01103F9702
#3 11:46:28 01103FA8DD
#4 11:47:17 01103F9702

To keep the first row in the output I used default value of lag as first(Timestamp) - 11 so that it satisfies the condition (difftime > 10) to select the row.

how can we remove the rows from xts based on the seconds criteria

You can first truncate the time and then remove duplicates. Since the 30 second elements are the non-unique elements, they get removed:

library(xts)
xts3 <- xts(x=rnorm(10), order.by=as.POSIXct(strptime("2021-11-04 05:57:00", "%Y-%m-%d %H:%M:%S")+1:10*30), born=as.POSIXct("1899-05-08"))

# Round observations in z to the next hour
index(xts3) <- as.POSIXct(trunc(index(xts3), units="mins"))

# Remove duplicate times in z
xts3_dup <- make.index.unique(xts3, drop = TRUE)

xts
2021-11-04 05:57:00 -0.19766541
2021-11-04 05:58:00 -0.00902353
2021-11-04 05:58:00 -2.56173420
2021-11-04 05:59:00 0.64355622
2021-11-04 05:59:00 -0.18794658
2021-11-04 06:00:00 0.03005718
2021-11-04 06:00:00 0.64367384
2021-11-04 06:01:00 0.74716446
2021-11-04 06:01:00 -0.29986731
2021-11-04 06:02:00 -0.57503711

> xts3_dup
[,1]
2021-11-04 05:57:00 -0.19766541
2021-11-04 05:58:00 -0.00902353
2021-11-04 05:59:00 0.64355622
2021-11-04 06:00:00 0.03005718
2021-11-04 06:01:00 0.74716446
2021-11-04 06:02:00 -0.57503711


Related Topics



Leave a reply



Submit