Keep only groups of data with multiple observations

前端 未结 2 512
既然无缘
既然无缘 2020-12-19 06:23

I am attempting to keep only deids with multiple observations.

I have the below code

help <- data.frame(deid = c(1, 5, 5, 5, 5, 5, 5, 12, 12, 12,         


        
2条回答
  •  无人及你
    2020-12-19 07:09

    Using data.table instead:

    helpcount <- help[, list(Count = .N), by = deid]
    helpf <- merge(help,helpcount, by = "deid")
    helpf <- helpf[Count > 1]
    

    EDIT: A bit more concise:

    help[, Count := .N, by = deid]
    help[Count > 1]
    

    EDIT2: thelatemail's even more concise solution:

    help[,if(.N > 1) .SD, by=deid]
    

提交回复
热议问题