I have this df
country Profit
Algeria 10614
Australia 36036
Hungary 2964
Sweden -26055
Australia 3777
Australia 15342
Hungary 12141
Sweden 15373
Algeria 98731
I want to count the country and sum profit each country
Like this
country count sumprofit
Australia 3 ....
Algeria 1 ....
Hungary 2 ....
Sweden 2 ....
CodePudding user response:
You can use -
library(dplyr)
df %>%
group_by(country) %>%
summarise(count = n(),
sumprofit = sum(Profit, na.rm = TRUE))
# country count sumprofit
# <chr> <int> <int>
#1 Algeria 2 109345
#2 Australia 3 55155
#3 Hungary 2 15105
#4 Sweden 2 -10682