1

I'm cleaning up the data set and I need to retain only those which repeated 4 times(like "a" and "b"), however, I am unable to do this. Can anyone please help?

Thank you!

let <- c("a","a","a","a","b","b","b","b","c","c","c","d","d","e")
avg <- c(1,1,1,2,3,4,5,6,1,2,3,4,3,5)

sample <- data.frame(let,avg)
PJG
  • 69
  • 7

1 Answers1

1

We can use data.table

library(data.table)
setDT(sample)[, .SD[.N >=4], let]
#   let avg
#1:   a   1
#2:   a   1
#3:   a   1
#4:   a   2
#5:   b   3
#6:   b   4
#7:   b   5
#8:   b   6

Or with base R using ave

sample[with(sample, ave(avg, let, FUN = length)>=4),]

Or with table

subset(sample, let %in% names(which(rowSums(table(sample)) >=4)))
akrun
  • 874,273
  • 37
  • 540
  • 662