Nic Hofmann
Nic Hofmann

Reputation: 3

How to average to 1 minute

I am trying to average to 1 min. In my data I have some data points that were taken within the same minute and this is not letting me plot my data. For example my data time stamps might look like:

2019-12-04 16:59:27 
2019-12-04 16:59:27 
2019-12-04 16:59:28
2019-12-04 16:59:29
2019-12-04 16:59:29
2019-12-04 16:59:30

How do I average this so that it consolidates those duplicate data points into a 1 min average?

Upvotes: 0

Views: 321

Answers (1)

R me matey
R me matey

Reputation: 685

For anything dates/times, the lubridate package is the way to go. In this case, you want round_date()

library(lubridate)
library(dplyr)
#First, create your dataset (at least, what I think it might look like)
df <- tibble(
  time = ymd_hms(c(
    "2019-12-04 16:59:27" ,
    "2019-12-04 16:59:27" , 
    "2019-12-04 16:59:28",
    "2019-12-04 16:59:29",
    "2019-12-04 16:59:29",
    "2019-12-04 16:59:30"
    ))
) %>%
  mutate(time = round_date(time, unit = "minutes")) %>% #Round the time variable to the nearest minute.
  distinct() #remove duplicate rows. 

The output:

# A tibble: 2 x 1
  time               
  <dttm>             
1 2019-12-04 16:59:00
2 2019-12-04 17:00:00

UPDATE: Looks like you're just looking for distinct rows, in which case just the distinct() function will do.

library(lubridate)
library(dplyr)
#First, create your dataset
df <- tibble(
  time = ymd_hms(c(
    "2019-12-04 16:59:27" ,
    "2019-12-04 16:59:27" , 
    "2019-12-04 16:59:28",
    "2019-12-04 16:59:29",
    "2019-12-04 16:59:29",
    "2019-12-04 16:59:30"
    ))
) %>%
  distinct() #remove duplicate rows. 

Output 2:

  time               
  <dttm>             
1 2019-12-04 16:59:27
2 2019-12-04 16:59:28
3 2019-12-04 16:59:29
4 2019-12-04 16:59:30

Upvotes: 1

Related Questions