I need to change the value of elements in a vector. But I want to change only the elements for which there are less then n instances.
I used this metodh, with Data$GENE being the vector to be changed.
Data$GENE[which(Data$GENE %in% names(table(Data$GENE)[table(Data$GENE) < 10]))] <<- 'other'
It's a bit convoluted, is there a more succint way?
UPDATE: answering to the comments below: actually is a quite easy case!
> vec <- c(rep('foo', 5), rep('foo1', 2), rep('foo2', 1), rep('foo3', 3), rep('bar', 6))
> table(vec)
vec
bar foo foo1 foo2 foo3
6 5 2 1 3
> vec[which(vec %in% names(table(vec)[table(vec) < 5]))] <- 'other'
> table(vec)
vec
bar foo other
6 5 6