1

I have a data frame which looks the following:

  Person  Rain        Route
1 1    no rain      4.6    
2 1    no rain      3
3 1    rain         20
4 2    rain         12
5 2    rain         3
6 2    no rain      5

so basically every Person has 3 rows.

Now, I want to sum the number of routes of every person based on the condition if it rained or not. Then I want to count the number of days it rained, or not. My desired output should look like this:

  Person  Rain        Route   frequencies
1 1       no rain      7.6        2
2 1       rain         20         1
3 2       rain         15         2
4 2       no rain      5          1

I am very grateful for any advice Thank you a lot

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66

1 Answers1

1
df = data.frame( Person = c(1,1,1,2,2,2),
  Rain = c("no rain","no rain",'rain','rain','rain',"no rain"),
                 Route = c(4.6,3,20, 12, 3,5)
)
library(dplyr)
df %>%
  group_by(Person, Rain) %>%
  mutate(Route = sum(Route)) %>%
  mutate(frequency = n()) %>%
  distinct()
Ali Fradi
  • 110
  • 1
  • 10