user10075864
user10075864

Reputation:

Adding a new column to the dataframe in R that contains the most frequent value in each row

for example consider the following dataframe:

    X[[i]] X[[i]] X[[i]]
1      1      1      1
2      1      1      2
3      1      2      2
4      0      3      0
5      3      3      3
6      0      3      0
7      4      3      4
8      4      4      4

the result will be:

    X[[i]] X[[i]] X[[i]] output
1      1      1      0      1
2      1      1      2      1
3      1      2      2      2
4      0      3      0      0
5      3      3      3      3
6      0      3      0      0
7      4      3      4      4
8      4      4      4      4

The dataframe vary in their number of rows and columns in each execution. And the output column values are numeric

Thanks in advance.

Upvotes: 1

Views: 150

Answers (2)

Aziz
Aziz

Reputation: 20705

What you're calculating is the mode of each row. The following will work for any number of rows and columns:

mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

df$output = apply(df, 1, mode)

This will produce:

  V1 V2 V3 output
1  1  1  1      1
2  1  1  2      1
3  1  2  2      2
4  0  3  0      0
5  3  3  3      3
6  0  3  0      0
7  4  3  4      4
8  4  4  4      4

Upvotes: 0

akrun
akrun

Reputation: 886938

We can loop over the rows with apply and use Mode

cbind(df1, output = apply(df1, 1, FUN = Mode))
#  X[[i]] X[[i]] X[[i]] output
#1      1      1      1      1
#2      1      1      2      1
#3      1      2      2      2
#4      0      3      0      0
#5      3      3      3      3
#6      0      3      0      0
#7      4      3      4      4
#8      4      4      4      4

where

Mode <- function(x) {
 ux <- unique(x)
 ux[which.max(tabulate(match(x, ux)))]
 }

data

df1 <- structure(list(`X[[i]]` = c(1L, 1L, 1L, 0L, 3L, 0L, 4L, 4L), 
    `X[[i]]` = c(1L, 1L, 2L, 3L, 3L, 3L, 3L, 4L), `X[[i]]` = c(1L, 
    2L, 2L, 0L, 3L, 0L, 4L, 4L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8"))

Upvotes: 2

Related Questions