Count unique values by group

bvowe
    DATA = data.frame("TRIMESTER" = c(1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3),
                      "STUDENT" = c(1,2,3,4,5,6,7,1,2,3,5,9,10,11,3,7,10,6,12,15,17,16,21))
    
    WANT = data.frame("TRIMESTER" = c(1,2,3),
                      "NEW_ENROLL" = c(7,3,5),
                      "TOTAL_ENROLL" = c(7,10,15))

I Have 'DATA' and want to make 'WANT' which has three columns and for every 'TRIMESTER' you count the number of NEW 'STUDENT' and then for 'TOTAL_ENROLL' you just count the total number of unique 'STUDENT' every trimester.

My attempt only counts the number for each TRIMESTER.

library(dplyr)
DATA %>%
group_by(TRIMESTER) %>%
count()
Rui Barradas

Here is a way.

suppressPackageStartupMessages(library(dplyr))

DATA <- data.frame("TRIMESTER" = c(1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3),
                  "STUDENT" = c(1,2,3,4,5,6,7,1,2,3,5,9,10,11,3,7,10,6,12,15,17,16,21))
DATA %>% 
  mutate(NEW_ENROLL = !duplicated(STUDENT)) %>%
  group_by(TRIMESTER) %>%
  summarise(NEW_ENROLL = sum(NEW_ENROLL)) %>%
  ungroup() %>%
  mutate(TOTAL_ENROLL = cumsum(NEW_ENROLL))
#> # A tibble: 3 × 3
#>   TRIMESTER NEW_ENROLL TOTAL_ENROLL
#>       <dbl>      <int>        <int>
#> 1         1          7            7
#> 2         2          3           10
#> 3         3          5           15

Created on 2022-08-14 by the reprex package (v2.0.1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count the number of unique values by group

From Dev

SQL group by count unique values into separate columns

From Dev

How to count the number of unique values in a group of rows?

From Dev

How to count the number of unique values by group?

From Dev

MongoDB - Count unique values with group by date

From Dev

R Help: Count Unique Values by Group

From Dev

Filter, Group by, and count unique values in each group in Power BI

From Dev

R: count unique values from group to group by date

From Dev

Assign count to unique values in in another column by group values in another column

From Dev

one year rolling count of unique values by group in pandas

From Dev

How to count the combinations of unique values per group in pandas?

From Dev

Pyspark, Group by count unique values in a column for a certain value in other column

From Dev

Powershell - Group and count unique values from CSV file based on a column

From Dev

SQL/Rails return count of unique values from a group of records

From Dev

Count of unique values per group as new column with pandas

From Dev

Count the number of unique values with at least k occurrences per group in postgres

From Dev

Pandas: syslog - group by location - count unique values by day

From Dev

Count unique values by group (data.table) doesn't work

From Dev

Python Count Number of Unique Values within Data frame within a group

From Dev

R - Count unique/distinct values in two columns together per group

From Dev

Count unique values over two columns per group

From Dev

Group the data by quarter and count the occurrence of each unique values in a column

From Dev

Count Unique Values in MongoDB

From Dev

Count unique values by column

From Dev

Count unique values

From Dev

Count unique values in a row

From Dev

Unique count of array values

From Dev

Json count unique values

From Dev

Group values by unique elements

Related Related

  1. 1

    Count the number of unique values by group

  2. 2

    SQL group by count unique values into separate columns

  3. 3

    How to count the number of unique values in a group of rows?

  4. 4

    How to count the number of unique values by group?

  5. 5

    MongoDB - Count unique values with group by date

  6. 6

    R Help: Count Unique Values by Group

  7. 7

    Filter, Group by, and count unique values in each group in Power BI

  8. 8

    R: count unique values from group to group by date

  9. 9

    Assign count to unique values in in another column by group values in another column

  10. 10

    one year rolling count of unique values by group in pandas

  11. 11

    How to count the combinations of unique values per group in pandas?

  12. 12

    Pyspark, Group by count unique values in a column for a certain value in other column

  13. 13

    Powershell - Group and count unique values from CSV file based on a column

  14. 14

    SQL/Rails return count of unique values from a group of records

  15. 15

    Count of unique values per group as new column with pandas

  16. 16

    Count the number of unique values with at least k occurrences per group in postgres

  17. 17

    Pandas: syslog - group by location - count unique values by day

  18. 18

    Count unique values by group (data.table) doesn't work

  19. 19

    Python Count Number of Unique Values within Data frame within a group

  20. 20

    R - Count unique/distinct values in two columns together per group

  21. 21

    Count unique values over two columns per group

  22. 22

    Group the data by quarter and count the occurrence of each unique values in a column

  23. 23

    Count Unique Values in MongoDB

  24. 24

    Count unique values by column

  25. 25

    Count unique values

  26. 26

    Count unique values in a row

  27. 27

    Unique count of array values

  28. 28

    Json count unique values

  29. 29

    Group values by unique elements

HotTag

Archive