mi3567
mi3567

Reputation: 79

R: how to account maximum duplicates in each elements of a vector

How can I account maximum duplicates in each elements of a vector Example: x <- c("a001", "a002,a003,a003", "a004,a004,a004", "a004,a006,a007") expected result is: c(1, 2, 3, 1)

Thanks in advance!

Upvotes: 0

Views: 49

Answers (4)

Martin Morgan
Martin Morgan

Reputation: 46856

sapply(strsplit(x, ","), function(x) max(table(x)))

and, for @mi3567's comment on @nicola's answer

lengths(lapply(strsplit(x, ","), unique))

Upvotes: 2

akrun
akrun

Reputation: 887088

Another option is

library(qdapTools)
do.call(pmax,mtabulate(strsplit(x, ',')))
#[1] 1 2 3 1

If we need the length of unique number of elements,

library(dplyr)
strsplit(x, ',') %>% 
         sapply(., n_distinct) 
#[1] 1 2 1 3

Upvotes: 2

Ven Yao
Ven Yao

Reputation: 3710

x <- c("a001", "a002,a003,a003", "a004,a004,a004", "a004,a006,a007")
y <- sapply(x, function(z){
  x1 <- unlist(strsplit(z, ","))
  return(rev(table(x1))[1])
})
unname(y)
# [1] 1 2 3 1

For your second question.

y1 <- sapply(x, function(z){
  x1 <- unlist(strsplit(z, ","))
  return(length(unique(x1)))
})
unname(y1)
# [1] 1 2 1 3

Upvotes: 0

nicola
nicola

Reputation: 24480

Just try:

vapply(strsplit(x,","), function(y) max(tapply(y,y,length)),1L)
#[1] 1 2 3 1

Upvotes: 2

Related Questions