我正在清理数据集,我只需要保留重复4次的数据集(例如“ a”和“ b”),但是,我无法做到这一点。谁能帮忙吗?
谢谢!
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)
答案 0 :(得分:1)
我们可以使用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
或者使用base R
的{{1}}
ave
或与sample[with(sample, ave(avg, let, FUN = length)>=4),]
table