Search code examples
rdplyr

Modify data with dplyr


I have a dataset that looks like this:

    ID  Main Size Distance
    1    1    2     0
    1    0    3     1
    1    0    4     2
    2    1    5     0
    2    0    6     2
    2    0    7     3

For each ID, and only for those IDs where Main is 0, I want to multiply size by distance, then sum up these results and store them in a new database that also includes ID. The result should be something like this:

    ID  Result
    1    11
    2    33

How can I do this? I tried different ways but I end up with NAs and NaNs. Thanks!


Solution

  • You could try

    df %>%
        summarise(Result = sum(Size * Distance * !Main), .by = ID)
    

    which gives

      ID Result
    1  1     11
    2  2     33