aggregate column data bay step

Hello
I have a two column table.
column1: Latitudes
column2: sardine
I want to add the values ​​of the sardine for each step of 0.5 of latitude.
that is to say add the value of the sardine for each 0.3 of latitude.
for example :
the sum of the sardine values ​​between latitude 25.0 and 25.3 will be assigned to latitude 25.0
and so on for all the latitude

Latitude --- sardine
24.02 --- 222
24.03 - -- 631
24.5 - -- 101
25.0 ---- 234
25.03 --- 5249
25.3 - --- 300

Hi wamal85, welcome to RStudio Community.

I think the floor() function could help here.

library(dplyr, warn.conflicts = FALSE)
#> Warning: package 'dplyr' was built under R version 4.0.4

df <- tibble(latitude = c(24.02, 24.03, 24.5, 25.0, 25.03, 25.3),
             sardine = c(222, 631, 101, 234, 5249, 300))

df %>% 
  group_by(latitude_floor = floor(latitude)) %>% 
  summarise(sardine_total = sum(sardine))
#> # A tibble: 2 x 2
#>   latitude_floor sardine_total
#>            <dbl>         <dbl>
#> 1             24           954
#> 2             25          5783

Created on 2021-03-17 by the reprex package (v1.0.0)

In future, it would be nice if you provide copy-and-paste-friendly data by preparing a reprex. Please read the guide below for a tutorial on how to do so.

This topic was automatically closed 21 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.