How to find the unique values in a column of an R data frame?

R ProgrammingServer Side ProgrammingProgramming

Categorical variables have multiple categories but if the data set is large and the categories are also large in numbers then it becomes a little difficult to recognize them. Therefore, we can extract unique values for categorical variables that will help us to easily recognize the categories of a categorical variable. We can do this by using unique for every column of an R data frame.

Example

Consider the below data frame −

> x1<-rep(c("A","B","C","D"),each=5)
> x2<-rep(c(5,10,15,20),times=c(2,8,6,4))
> x3<-rep(c("India","Russia","China","Indonesia","Iceland"),times=c(4,3,5,2,6))
> x4<-rep(c(letters[1:10]),times=2)
> df<-data.frame(x1,x2,x3,x4)
> df
   x1 x2  x3     x4
1  A  5 India     a
2  A  5 India     b
3  A 10 India     c
4  A 10 India     d
5  A 10 Russia    e
6  B 10 Russia    f
7  B 10 Russia    g
8  B 10 China     h
9  B 10 China     i
10 B 10 China     j
11 C 15 China     a
12 C 15 China     b
13 C 15 Indonesia c
14 C 15 Indonesia d
15 C 15 Iceland   e
16 D 15 Iceland   f
17 D 20 Iceland   g
18 D 20 Iceland   h
19 D 20 Iceland   i
20 D 20 Iceland   j

Finding the unique values in column x1 −

> unique(df[c("x1")])
x1
1 A
6 B
11 C
16 D

Finding the unique values in column x2 −

> unique(df[c("x2")])
x2
1 5
3 10
11 15
17 20

Finding the unique values in column x3 −

> unique(df[c("x3")])
x3
1 India
5 Russia
8 China
13 Indonesia
15 Iceland

Finding the unique values in column x4 −

> unique(df[c("x4")])
x4
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 j
raja
Published on 11-Aug-2020 08:46:15
Advertisements