1

I have the following data table:

n    col1
1    c('1', '1', '2')
2    1
3    c('3', '3', '1')
4    3

And as an output, I want to only have only the most frequent character appearing in each of the rows.So, it would be something like this:

 n    col2
 1    1
 2    1
 3    3
 4    3

I don't really know how to approach this problem in R. It should be something like finding the most frequent value in the list. I googled that there is table() function, but I can't figure out how I should use it here. It doesn't look like something really hard, but I just can't figure it out

1 Answers1

2

We can use the Mode from here

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

df1$col2 <- sapply(df1$col1, Mode)
df1$col2
#[1] "1" "1" "3" "3"

Or with tidyverse

library(dplyr)
library(purrr)
df1 %>%
    transmute(n, col2 = map_chr(col1, Mode))

data

df1 <- data.frame(n = 1:4, col1 = I(list(c('1', '1', '2'), '1', 
      c('3', '3', '1'), '3')))
akrun
  • 874,273
  • 37
  • 540
  • 662