Different Ways To Count Unique Values In R | by Chi Nguyen | Towards Data Science

I was originally a Python user. I use Python in my daily work to explore, visualize and analyze data. However, at the moment, as I am undertaking my Master in Statistics, it is required that I learn R. As R is an open source program with about 10,000 libraries and packages contributed by different developers and statisticians, it lacks the consistency to develop an algorithm to a specific problem. As such, at first, I found it quite difficult to get familiar with R syntax and its approaches to various problems. In this article, I will show you a simple example of how values are aggregated by different libraries in R. I hope you will find this useful as you begin you-R journey
We will need several packages, which include dplyr and data.table.
#Installing Packagesinstall.packages("dplyr")install.packages("data.table")#Loading Packageslibrary('dplyr')library("data.table")
I am going to use dataset of a Grocery Store downloaded from Kaggle. For your reference, you can download the data in the link below:
www.kaggle.com
Let’s me show some information of the data.
I will transform the Date of DATA table into a new format YYYY-MM, and store the new column of Date format as month_year
DF1 <- DATA DF1$month_year <- format(as.Date(DF1$Date, format = "%d-%m-%Y"), "%Y-%m")
There are several solutions for this calculation and I am going to show you some.
Counting Unique Values by Using aggregate() Function of Base R
Counting Unique Values by Using aggregate() Function of Base R
group_by_month <-aggregate(x = DF1$Member_number,          # Specify data column          by = list(DF1$month_year),      # Specify group indicator          FUN = function(x) length(unique(x))) #Desired function
Or
group_by_month1 <- aggregate(Member_number ~ month_year, #Data column group by period          data = DF1,           FUN = function(x) length(unique(x)))
2. Counting Unique Values by Using group_by() & summarise Functions of dplyr Package
group_by_month_2 <- DF1 %>%                             #Applying group_by &summarisegroup_by(month_year) %>%summarise(unique_customer = n_distinct(Member_number))
3. Counting Unique Values by Using length() & unique() Functions & data.table Package
group_by_month_3 <- data.table(DF1)[ , .(unique_cus = length(unique(Member_number))), by = month_year]
The results for 3 different ways are the same as you can see in the picture below for the first 5 rows:
Using group_by() & summarise Functions of dplyr Package
Using group_by() & summarise Functions of dplyr Package
group_by_month_2a <- DF1%>%group_by(month_year) %>%summarise(unique_customer = n_distinct(Member_number),          number_of_entries = n(),          unique_product = n_distinct(itemDescription))
2. Using length() & unique() Functions & data.table Package
group_by_month_3a <-data.table(DF1)[,  list(unique_customer = n_distinct(Member_number),number_of_entries = .N,unique_product = n_distinct(itemDescription)), by=month_year]
In the end, both generate the same result:
With many different packages supported, R gives users an advantage to develop different algorithms and modules to solve problems. R makes hard problems easier with various approaches, though sometimes it also complicates simple problems. As an R beginner, I find these different packages and syntax quite confusing to pick up.
Despite this confusion, I will still try to learn R as it is an effective statistical tool, and of course it will benefit my grades in grad school :D