1

I have this dataframe

my dataframe

where values in the 'Age' columns need to be summarize per the whole size range i.e. now the data frame is like this:

     Size   Age 1   Age 2  Age 3
[1]    8      2       8      5
[2]   8.5     4       7      9
[3]    9      1       11     45
[4]   9.5     3        2      0

But i want this

       Size   Age 1   Age 2  Age 3
[1+2]   8      6       15     16  
[3+4]   9      4       13     45 

Which function is better to use in R?

I thought but I don't tried, to use rowwise () together with mutate (), but I don't know how to set the criteria.

Thank you in advance for the help :)

  • 1
    Welcome to SO, Valentina Caserta! Please do not post (only) an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/). Please include the code, console output, or data (e.g., `data.frame(...)` or the output from `dput(head(x))`) directly. – r2evans Nov 18 '22 at 13:52

1 Answers1

2

You can do this quite easily with the dplyr library. (You may need to install.packages("dplyr") if you haven't already.)

Using dplyr functions, we can group by a new grouping column, size, replacing the existing size column with values that have been rounded down to the nearest whole number. Then we just summarise across all the columns that starts_with "Age" and sum up the values.

require(dplyr)

my_df |>
  group_by(size = floor(size)) |>
  summarise(
    across(starts_with("Age"), sum)
  )
        
Captain Hat
  • 2,444
  • 1
  • 14
  • 31