0

I have a long dataframe, that consists of the identifier for a group and a corresponding value. See an example below:

FamilyMass     
Family  Body.Mass
    1   Chrysochloridae 22.04
    2   Chrysochloridae 24.05
    3   Chrysochloridae 52.34
    4   Chrysochloridae 38.30
    5   Chrysochloridae 37.16
    6   Chrysochloridae 55.76
    7   Chrysochloridae 434.04
    8   Chrysochloridae 108.35
    9   Chrysochloridae 21.99
    10  Chrysochloridae 62.60
    11  Tenrecidae  152.25
    12  Tenrecidae  6.69

I need to calculate the cumsum of all values in the second column for each batch of elements with the same value in the first column. So, in the first case, I would calculate the cumsum for the first 10 values, as they all correspond to Chrysochloridae, and then calculate the cumsum for the following two entries and store them in a different place.

I tried breaking the dataframe into smaller lists, each contaning a vector with the respective Body Mass entries using a loop, but so far I haven't succeeded.

Is there any way to solve the problem? Forgive me if it's a basic question, I'm very new to R and programming in general.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Po1sN3
  • 1
  • 1

0 Answers0