How to make a unique in R by column A and keep the row with maximum value in column B

前端 未结 2 602
情歌与酒
情歌与酒 2021-01-13 12:29

I have a data.frame with several columns (17). Column 2 have several rows with the same value, I want to keep only one of those rows, specifically the one that has the maxim

相关标签:
2条回答
  • 2021-01-13 12:57

    A not so elegant solution using R base functions

    > ind <- with(dat, tapply(B, A, which.max)) # Using @Roland's data
    > mysplit <- split(dat, dat$A)
    > do.call(rbind, lapply(1:length(mysplit), function(i) mysplit[[i]][ind[i],]))
      A   B         C
    3 a   3 0.3631284
    5 b 200 0.4042683
    
    0 讨论(0)
  • 2021-01-13 13:04

    A solution using package data.table:

    set.seed(42)
    dat <- data.frame(A=c('a','a','a','b','b'),B=c(1,2,3,5,200),C=rnorm(5))
    library(data.table)
    
    dat <- as.data.table(dat)
    dat[,.SD[which.max(B)],by=A]
    
       A   B         C
    1: a   3 0.3631284
    2: b 200 0.4042683
    
    0 讨论(0)
提交回复
热议问题