I have a data frame and I would like to count the number of different observations per group, not counting the NA values.
Here is an example of the data:
ID <-c("A", "A", "B", "B", "B", "C")
Act1 <- c("Football", "Swim", "Football", 'Basketball', "Swim", "Tennis")
Act2 <- c("Swim", "Football", "Tennis", 'Swim', "Football", "Swim")
Act3 <- c("NA", "Tennis", "NA", 'Football', "Tennis", "NA")
df <- data.frame(ID,Act1, Act2, Act3)
df
ID Act1 Act2 Act3
1 A Football Swim NA
2 A Swim Football Tennis
3 B Football Tennis NA
4 B Basketball Swim Football
5 B Swim Football Tennis
6 C Tennis Swim NA
The correct answer would look like this...
ID n
1 A 3
2 B 4
3 C 2
Because A has three different activities (e.g. football, swim, tennis), B has four (e.g. football, swim, tennis, basketball) and C has two (e.g. tennis and swim)
How could I do that?