Average value on several column

Hi all,
I have this kind of dataset,
What I would like it's to each interval and different date inside each interval, have the average depth and the average value, so like have just one line per date with average depth and value, and this for all the different date in all different interval.

df<- tribble(
~Interval, ~Value, ~Depth, ~Date
1,	-96.14,	10,	20211013
1,	-94.62,	20,	20211013
1,	-92.41,	30,	20211014
1,	-91.27, 40, 20211014
2,	-84.14,	50,	20211017
2,	-74.91,	60,	20211017
2,	-72.61,	70,	20211017
2,	-69.74,	80,	20211018
2,	-69.73,	90,	20211018
3,	-73.83,	100,20211019
3,	-77.27,	110,20211019
3,	-79.79,	120,20211019
3,	-80.89,	130,20211020
3,	-82.44,	140,20211020
4,	-82.43,	150,20211023
4,	-80.94,	160,20211023
4,	-80.11	170,20211025
4,	-78.18,	180,20211025

How can I do that ?
Thanks a lot

Is this the kind of thing you are looking for?

library(dplyr)
library(tibble)
#> Warning: package 'tibble' was built under R version 4.1.2

df <- tribble(
  ~Interval, ~Value, ~Depth, ~Date,
  1,    -96.14, 10, 20211013,
  1,    -94.62, 20, 20211013,
  1,    -92.41, 30, 20211014,
  1,    -91.27, 40, 20211014,
  2,    -84.14, 50, 20211017,
  2,    -74.91, 60, 20211017,
  2,    -72.61, 70, 20211017,
  2,    -69.74, 80, 20211018,
  2,    -69.73, 90, 20211018,
  3,    -73.83, 100,20211019,
  3,    -77.27, 110,20211019,
  3,    -79.79, 120,20211019,
  3,    -80.89, 130,20211020,
  3,    -82.44, 140,20211020,
  4,    -82.43, 150,20211023,
  4,    -80.94, 160,20211023,
  4,    -80.11, 170,20211025,
  4,    -78.18, 180,20211025)

dfStats <- df |> group_by(Interval, Date) |> 
  summarise(AvgValue = mean(Value), AvgDepth = mean(Depth))
#> `summarise()` has grouped output by 'Interval'. You can override using the
#> `.groups` argument.

dfStats  
#> # A tibble: 8 x 4
#> # Groups:   Interval [4]
#>   Interval     Date AvgValue AvgDepth
#>      <dbl>    <dbl>    <dbl>    <dbl>
#> 1        1 20211013    -95.4       15
#> 2        1 20211014    -91.8       35
#> 3        2 20211017    -77.2       60
#> 4        2 20211018    -69.7       85
#> 5        3 20211019    -77.0      110
#> 6        3 20211020    -81.7      135
#> 7        4 20211023    -81.7      155
#> 8        4 20211025    -79.1      175

Created on 2022-07-06 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.