Remove duplicates keeping entry with largest absolute value

前端 未结 7 2384
醉酒成梦
醉酒成梦 2020-11-28 10:12

Let\'s say I have four samples: id=1, 2, 3, and 4, with one or more measurements on each of those samples:

> a <- data.frame(id=c(1,1,2,2,3,4), value=c         


        
7条回答
  •  心在旅途
    2020-11-28 10:32

    A data.table approach might be in order if your data set is very large:

    library(data.table)
    
    aDT <- as.data.table(a)
    setkey(aDT,"id")
    
    aDT[J(unique(id)), list(value = value[which.max(abs(value))])]
    


    Or a not as fast, but still fast, alternative :

    library(data.table)
    as.data.table(a)[, .SD[which.max(abs(value))], by=id]
    

    This version returns all the columns of a, in case there are more in the real dataset.

提交回复
热议问题