How to remove rows that have only 1 combination for a given ID

前端 未结 4 1863
时光说笑
时光说笑 2020-12-04 02:59

I have a dataframe like this

ID <- c(\"A\",\"A\",\"A\",\"B\",\"B\",\"C\",\"C\")
Measurement <- c (\"Length\",\"Breadth\",\"Breadth\",\"Breadth\",\"Leng         


        
4条回答
  •  不知归路
    2020-12-04 03:32

    Using data.table:

    library(data.table)
    DT <- data.table(df)
    DT[, Count := length(unique(Measurement)), ID][Count > 1]
    

    Edit

    Alternatively, a much better one-liner suggested by @DavidArenburg:

    setDT(df)[, if(uniqueN(Measurement) > 1) .SD, by = ID]
    

提交回复
热议问题