David Potrel
David Potrel

Reputation: 111

Cross table with filtered obs on R

I'd like to make a crosstable where x is the number of distinct obs of a variable and y is an other variable. With dplyr I can get the information I need with the code below but it obviously does not create a table. What I would like is the number of distinct obs of CODE_UAI.x for secteur. Below is an example of the data set.

CODE_UAI.x secteur
0271564D 1
0942344M 2
0271564D 3
0332894S 3
0033082C 2
00381324H 1
0271564D 3
0033082C 2
df1 <- data.frame("CODE_UAI.x" = c("0271564D", "0942344M", "0271564D", "0332894S",
                                   "0033082C", "00381324H", "0271564D", "0033082C"),
                  secteur = c(1, 2, 3, 3, 2, 1, 3, 2))
Ech_final_nom_BSA %>%
  filter(secteur == 3) %>%
  summarise(n=n_distinct(CODE_UAI.x))

I've also tried doing this with the package summarytools with no results.

The results I'm expecting is something like this:

Secteur # distinct UAI
1 2
2 2
3 2

I hope this makes sense. Feel free to ask further information if needed.

Upvotes: 0

Views: 335

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388962

Instead of using filter, you can instead do group_by for each secteur and count unique values using n_distinct.

library(dplyr)
df1 %>% group_by(secteur) %>% summarise(n=n_distinct(CODE_UAI.x))

#  secteur     n
#    <dbl> <int>
#1       1     2
#2       2     2
#3       3     2

Or in base R -

aggregate(CODE_UAI.x~ secteur, df1, function(x) length(unique(x)))

Upvotes: 0

Related Questions