How to find the unique values in a column of R dataframe?
In this article, we will discuss how to find out the unique value in a column of dataframe in R Programming language. For this task, unique() function is used where the column name is passed for which unique values are to be printed.
Syntax: unique(x)
parameters:
- x: data frame
For a column name select the column using $
dataframe$column_name
Example 1:
R
id<- c(1,2,3,4,5,6,7,8,9) country <- c("INDIA","AMERICA","JAPAN","CHINA","BANGLADESH", "SRILANKA","NEPAL","AMERICA","CHINA") data<-data.frame(id,country) unique(data$country) |
Output:
[1] INDIA AMERICA JAPAN CHINA BANGLADESH SRILANKA NEPAL
Levels: AMERICA BANGLADESH CHINA INDIA JAPAN NEPAL SRILANKA
Example 2:
R
data <- data.frame(x1 = c(9, 5, 6, 8, 9), x2 = c(2, 4, 2, 7, 1), x3 = c(3,6,7,0,3), x4 = c("Hello", "value", "value", "geeksforgeeks", NA) ) unique(data[c("x2")]) unique(data[c("x1")]) |
Output:
Unique data in column x2
x2
1 2
2 4
4 7
5 1
Unique data in column x1
x1
1 9
2 5
3 6
4 8
Example 3:
R
data<- data.frame(Student=c('John','Lee','Guy', 'John','Lee','Guy'), Age=c(18,19,20,18,19,20), Gender=c('Male','Female','Male', 'Male','Female','Male')) unique(data) |
Output:


