How to find the row wise mode of strings in an R data frame?


To find the row wise model of strings in an R data frame, we can use apply function along with custom function for mode, if ties will be there then first value will be chosen based on alphabetical ordering.

For Example, if we have a data frame called df that contains string values then we can find the row wise mode of strings by using the command given below −

df$RowM<-apply(df[], 1, function(x) {names(which.max(table(factor(x,unique(x)))))})

Example 1

Following snippet creates a sample data frame −

x1<-sample(letters[1:5],20,replace=TRUE)
x2<-sample(letters[1:5],20,replace=TRUE)
x3<-sample(letters[1:5],20,replace=TRUE)
x4<-sample(letters[1:5],20,replace=TRUE)
df1<-data.frame(x1,x2,x3,x4)
df1

The following dataframe is created

  x1 x2 x3  x4
1  b a  c   e
2  c b  c   a
3  b d  d   d
4  d a  a   e
5  a b  c   c
6  a b  e   e
7  d d  e   a
8  c b  d   a
9  c a  c   d
10 c e  e   a
11 d a  a   c
12 a b  c   a
13 a d  d   c
14 e a  e   b
15 d b  a   b
16 d a  a   d
17 d a  a   d
18 c d  b   c
19 c e  c   b
20 c e  c   e

To find row wise mode for each row in df1 on the above created data frame, add the following code to the above snippet −

x1<-sample(letters[1:5],20,replace=TRUE)
x2<-sample(letters[1:5],20,replace=TRUE)
x3<-sample(letters[1:5],20,replace=TRUE)
x4<-sample(letters[1:5],20,replace=TRUE)
df1<-data.frame(x1,x2,x3,x4)
df1$Row_Mode<-apply(df1[], 1, function(x) {names(which.max(table(factor(x,unique(x)))))})
df1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

  x1 x2 x3 x4 Row_Mode
1  b a  c   e   b
2  c b  c   a   c
3  b d  d   d   d
4  d a  a   e   a
5  a b  c   c   c
6  a b  e   e   e
7  d d  e   a   d
8  c b  d   a   c
9  c a  c   d   c
10 c e  e   a   e
11 d a  a   c   a
12 a b  c   a   a
13 a d  d   c   d
14 e a  e   b   e
15 d b  a   b   b
16 d a  a   d   d
17 d a  a   d   d
18 c d  b   c   c
19 c e  c   b   c
20 c e  c   e   c

Example 2

Following snippet creates a sample data frame −

y1<-sample(c("Low","Medium","High"),20,replace=TRUE)
y2<-sample(c("Low","Medium","High"),20,replace=TRUE)
y3<-sample(c("Low","Medium","High"),20,replace=TRUE)
df2<-data.frame(y1,y2,y3)
df2

The following dataframe is created

   y1      y2    y3
1  High   Low    Low
2  Low    High   High
3  Low    Low    High
4  High   Medium Low
5  High   Medium Low
6  Medium Low    Medium
7  Low    High   Low
8  Low    High   Low
9  Medium High   Medium
10 High   High   High
11 Low    Medium Low
12 Low    Low    Low
13 Medium High   High
14 High   Low    Low
15 High   Medium High
16 High   High   High
17 Low    Medium High
18 Low    Low    Medium
19 Medium Medium Low
20 Medium Medium High

To find row wise mode for each row in df2 on the above created data frame, add the following code to the above snippet −

y1<-sample(c("Low","Medium","High"),20,replace=TRUE)
y2<-sample(c("Low","Medium","High"),20,replace=TRUE)
y3<-sample(c("Low","Medium","High"),20,replace=TRUE)
df2<-data.frame(y1,y2,y3)
df2$Row_Mode<-apply(df2[], 1, function(x) {names(which.max(table(factor(x,unique(x)))))})
df2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

     y1     y2    y3    Row_Mode
1  High   Low    Low    Low
2  Low    High   High   High
3  Low    Low    High   Low
4  High   Medium Low    High
5  High   Medium Low    High
6  Medium Low    Medium Medium
7  Low    High   Low    Low
8  Low    High   Low    Low
9  Medium High   Medium Medium
10 High   High   High   High
11 Low    Medium Low    Low
12 Low    Low    Low    Low
13 Medium High   High   High
14 High   Low    Low    Low
15 High   Medium High   High
16 High   High   High   High
17 Low    Medium High   Low
18 Low    Low    Medium Low
19 Medium Medium Low    Medium
20 Medium Medium High   Medium

Updated on: 03-Nov-2021

329 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements