Search code examples
rtm

How do I return NA if there is no most common string, and store the most common string (if exists) in a separate column?


Here's an example of my data:

v1 <- c("Friday", "Wednesday")
v2 <- c("Sunday", "Monday", "Sunday", "Friday")
mylist <- list(v1, v2)

For [[1]], it should return something like NA. For [[2]], it should return Sunday

I've tried using R's tm package:

termFreq(mylist[[2]])

which returns the frequency table. I want to extract the element with maximum frequency and save it in another column. In the case where there is no maximum (such as in [[1]], I want to return NA.

Any help would be appreciated!


Solution

  • We create a Mode function and then apply it on the list elements

    Mode <- function(x) {
     ux <- unique(x)
     count = tabulate(match(x, ux))
      if(length(unique(count)) == 1) {
       NA
     } else ux[which.max(count)]
    }
    
    lapply(mylist, Mode)
    #[[1]]
    #[1] NA
    
    #[[2]]
    #[1] "Sunday"