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 |
- Code for input
dataframe
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.
Instead of using
filter, you can instead dogroup_byfor eachsecteurand count unique values usingn_distinct.Or in base R -