R function which.max with tapply

后端 未结 3 1372
独厮守ぢ
独厮守ぢ 2021-01-19 11:37

I am trying to make a data frame with the maximum over records by a factor. I would like a data frame with 4 rows (one for each G) with the max for X in that group and the c

3条回答
  •  执笔经年
    2021-01-19 12:28

      library(data.table)
      set.seed(1)
      Data<-data.frame(X=rnorm(200), Y=rnorm(200), G=rep(c(1,2,3,4), each=50))
      setDT(Data)[,list(X=max(X),Y=Y[which.max(X)]),by=G]
       G        X          Y
    1: 1 1.595281 -0.3309078
    2: 2 2.401618  0.9510128
    3: 3 2.087167  0.9160193
    4: 4 2.307978 -0.3887222
    

提交回复
热议问题