Aggregate time series data, get average without NA or 0s in R

Viewed 115

I am trying to aggregate my time series data, and I want to get the average value of the aggregated value without including NAs or 0s.

Here is what my data looks like.

                     V1      423 470  473 626
 1: 2018-01-01 00:00:00  0.00000   0  0.0   0
 2: 2018-01-01 00:01:00  8.00000   0 95.0   0
 3: 2018-01-01 00:02:00  0.00000   0  0.0   0
 4: 2018-01-01 00:03:00 31.00000   0 24.5   0
 5: 2018-01-01 00:04:00 37.00000  28 33.0  31

I am trying to aggregate in 5 minute interval, and my expected output is

                   V1      423 470  473 626
  2018-01-01 00:05:00 34.00000  28 50.8  31
                         ~
: 2018-01-01 00:10:00        A   B    C   D

How do I aggregate them in 5 minute interval while getting the average value excluding 0 s or NA s ?

EDIT

structure(list(V1 = c("2018-01-01 00:00:00", "2018-01-01 00:01:00", 
"2018-01-01 00:02:00", "2018-01-01 00:03:00", "2018-01-01 00:04:00", 
"2018-01-01 00:05:00", "2018-01-01 00:06:00", "2018-01-01 00:07:00", 
"2018-01-01 00:08:00", "2018-01-01 00:09:00", "2018-01-01 00:10:00", 
"2018-01-01 00:11:00", "2018-01-01 00:12:00", "2018-01-01 00:13:00", 
"2018-01-01 00:14:00", "2018-01-01 00:15:00", "2018-01-01 00:16:00", 
"2018-01-01 00:17:00", "2018-01-01 00:18:00", "2018-01-01 00:19:00"
), `423` = c(0, 8, 0, 31, 37, 31, 26.1111111111111, 39.375, 35.5, 
19.3, 21.5454545454545, 41.2, 31, 27.375, 31, 24.3076923076923, 
26.1666666666667, 24, 26.8, 30.8181818181818), `470` = c(0, 0, 
0, 0, 28, 0, 0, 0, 27, 21, 0, 21.5, 0, 0, 0, 0, 10, 46, 19.5, 
0), `473` = c(0, 95, 0, 24.5, 33, 55, 50, 0, 47, 45, 0, 0, 35.4, 
0, 0, 23, 32.5, 0, 0, 55), `626` = c(0, 0, 0, 0, 31, 26, 0, 0, 
0, 16, 0, 0, 0, 0, 75, 0, 0, 48, 0, 0)), row.names = c(NA, -20L
), .internal.selfref = <pointer: 0x0000029131ff1ef0>, class = c("data.table", 
"data.frame"))
2 Answers

Does this work:

> df %>% mutate(ID = rep(letters[1:ceiling(nrow(df)/5)], each = 5)) %>% 
+               group_by(as.numeric(as.factor(ID))) %>% 
+                       select(-c(v1,ID)) %>% summarise(across(`423`:`625`,~ mean(.x[which(.x>0)]))) %>% 
+                                 select(-1) %>% mutate(v1 =  seq.POSIXt(ymd_hms('2018-01-01 00:05:00'), by = '5 mins', length.out = n()))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 2 x 5
  `423` `470` `473` `625` v1                 
  <dbl> <dbl> <dbl> <dbl> <dttm>             
1  25.3    28  50.8    31 2018-01-01 00:05:00
2  20.7    26  49.7    32 2018-01-01 00:10:00
> 

Data used:

> df
# A tibble: 10 x 5
   v1                  `423` `470` `473` `625`
   <dttm>              <dbl> <dbl> <dbl> <dbl>
 1 2018-01-01 00:00:00     0     0   0       0
 2 2018-01-01 00:01:00     8     0  95       0
 3 2018-01-01 00:02:00     0     0   0       0
 4 2018-01-01 00:03:00    31     0  24.5     0
 5 2018-01-01 00:04:00    37    28  33      31
 6 2018-01-01 00:05:00     0     0   0       0
 7 2018-01-01 00:06:00     8     0  95       0
 8 2018-01-01 00:07:00     0     0   0       0
 9 2018-01-01 00:08:00    30     0  20       0
10 2018-01-01 00:09:00    24    26  34      32
> 

Running on new Data:

> dput(BB)
structure(list(V1 = structure(c(1514764800, 1514764860, 1514764920, 
1514764980, 1514765040, 1514765100, 1514765160, 1514765220, 1514765280, 
1514765340, 1514764800, 1514764860, 1514764920, 1514764980, 1514765040, 
1514765100, 1514765160, 1514765220, 1514765280, 1514765340), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), `423` = c(0, 8, 0, 31, 37, 31, 26.1111111111111, 
39.375, 35.5, 19.3, 21.5454545454545, 41.2, 31, 27.375, 31, 24.3076923076923, 
26.1666666666667, 24, 26.8, 30.8181818181818), `470` = c(0, 0, 
0, 0, 28, 0, 0, 0, 27, 21, 0, 21.5, 0, 0, 0, 0, 10, 46, 19.5, 
0), `473` = c(0, 95, 0, 24.5, 33, 55, 50, 0, 47, 45, 0, 0, 35.4, 
0, 0, 23, 32.5, 0, 0, 55), `626` = c(0, 0, 0, 0, 31, 26, 0, 0, 
0, 16, 0, 0, 0, 0, 75, 0, 0, 48, 0, 0)), row.names = c(NA, -20L
), class = c("data.table", "data.frame"))
> BB$V1 <- ymd_hms(BB$V1)
> BB %>% mutate(ID = rep(letters[1:ceiling(nrow(BB)/5)], each = 5)) %>% 
+   group_by(as.numeric(as.factor(ID))) %>% 
+   select(-c(V1,ID)) %>% summarise(across(`423`:`626`,~ mean(.x[which(.x>0)]))) %>% 
+   select(-1) %>% mutate(V1 =  seq.POSIXt(ymd_hms('2018-01-01 00:05:00'), by = '5 mins', length.out = n()))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 4 x 5
  `423` `470` `473` `626` V1                 
  <dbl> <dbl> <dbl> <dbl> <dttm>             
1  25.3  28    50.8    31 2018-01-01 00:05:00
2  30.3  24    49.2    21 2018-01-01 00:10:00
3  30.4  21.5  35.4    75 2018-01-01 00:15:00
4  26.4  25.2  36.8    48 2018-01-01 00:20:00
> 

The following uses cut to make of column V1 a grouping variable by 5 minutes intervals and then summarizes using a custom function to compute means without NA's or zero values. I have left this function in two code lines to make it more readable but it could simply be

f <- function(x) mean(x[x != 0], na.rm = TRUE)

The date time column V1 is first coerced to class "POSIXct".

library(data.table)

f <- function(x){
  y <- x[x != 0]
  mean(y, na.rm = TRUE)
}

df[, V1 := as.POSIXct(V1)]
df[, V1 := cut(V1, "5 mins")]
df[, lapply(.SD, f), by = V1]
#                    V1      423      470      473 626
#1: 2018-01-01 00:00:00 25.33333 28.00000 50.83333  31
#2: 2018-01-01 00:05:00 30.25722 24.00000 49.25000  21
#3: 2018-01-01 00:10:00 30.42409 21.50000 35.40000  75
#4: 2018-01-01 00:15:00 26.41851 25.16667 36.83333  48

A one-liner could be

df[, lapply(.SD, f), by = cut(as.POSIXct(V1), "5 mins")]
Related