I have this data base:
# A tibble: 20 × 4
country category work pay
<chr> <dbl> <chr> <dbl>
1 Italy 0 Yes 20
2 France 0 False 10
3 France 0 Yes 25
4 Italy 0 Yes 10
5 Italy 1 Yes 25
6 France 0 False 10
7 France 0 Yes 15
8 Italy 0 False 25
9 Italy 0 False 25
10 France 0 Yes 20
11 Italy 1 False 10
12 France 0 False 10
13 Italy 0 Yes 20
14 Italy 1 Yes 10
15 Italy 0 False 20
16 France 0 Yes 20
17 France 1 Yes 10
18 France 0 Yes 10
19 France 0 Yes 10
20 Italy 0 Yes 15
I need to group by country and category and then add the values of the "pay" category for each group and subtract 10 from the result, but only for those with a "Yes" in the work column.
How can this be done?
I have been doing this:
exa %>%
group_by(country, category) %>%
filter(work=="Yes") %>%
summarize(sum= sum(pay,
na.rm=T) - n())
But this doesnt work because it calculate the n after the filter is applied. And I need the n() before that.
This are the n() I need to work with:
# A tibble: 4 × 3
# Groups: country [2]
country category n
<chr> <dbl> <int>
1 France 0 9
2 France 1 1
3 Italy 0 7
4 Italy 1 3