Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of unique levels of a variable

Tags:

dataframe

r

I am trying to get a simple way to count the number of distinct categories in a column of a dataframe.

For example, in the iris data frame, there are 150 rows with one of the columns being species, of which there are 3 different species. I want to be able to run this bit of code and determine that there are 3 different species in that column. I do not care how many rows each of those unique entries correspond to, just how many distinct variables there are, which is mostly what I found in my research.

I was thinking something like this:

df <- iris
choices <- count(unique(iris$Species))

Does a solution as simple as this exist? I have looked at these posts, but they either examine the entire data frame rather than a single column in that data frame or provide a more complicated solution than what I am hoping for.

count number of instances in data frame

Count number of occurrences of categorical variables in data frame (R)

How to count number of unique character vectors within a subset of data

like image 563
User247365 Avatar asked Jul 21 '16 00:07

User247365


People also ask

How do you count unique values in a data set?

You can use the combination of the SUM and COUNTIF functions to count unique values in Excel. The syntax for this combined formula is = SUM(IF(1/COUNTIF(data, data)=1,1,0)). Here the COUNTIF formula counts the number of times each value in the range appears.

How do I count unique values in a variable in R?

Method 1: Using length(unique()) function Unique() function when provided with a list will give out only the unique ones from it. Later length() function can calculate the frequency. Example 1: R.

How do I count unique values in Excel?

To count distinct values in excel, first enter the formula =SUM(1/COUNTIF(range, range)) in the desired cell. The range specifies the starting cell and ending cell separated by a colon. This is an array function, so press Ctrl+Shift+Enter to apply the formula.


1 Answers

The following should do the job:

choices <- length(unique(iris$Species))
like image 170
Imran Ali Avatar answered Oct 05 '22 21:10

Imran Ali