Counting unique combination of columns in a data frame for each country in R




below is my data frame… I want to count the medals for each country (denoted in NOC column)…
Since there are team events (here every row corresponds to one athlete) i have to count unique event medals.

SO I am trying to count Combination of Event, Event_gender, Medal with following code.

y= ddply(data1_2000 , .(NOC), function(df) count(df[,c(“Event”,“Event_gender”,“Medal”) ]))

I also tried
z= sqldf(“select NOC,COUNT(distinct Event,Event_gender,Medal) as MedalCount from data1_2000 group by NOC”)

here am getting an error :
Error in sqliteSendQuery(con, statement, :
error in statement: wrong number of arguments to function COUNT()

But am not getting the desired result.

please suggest somethng.


Hi Ashi,
I shall use dplyr for this and do

medalpercountry <-data1_2000 %>% group_by( NOC, Event, Event_gender, Medal %>% summerise(n())

It should give you your resuls.