Selecting Max Column Values in R

后端 未结 2 857
甜味超标
甜味超标 2020-12-11 20:43

I\'m pretty new to R and have a question regarding selecting the max values in a column.

I have the following data frame:

          X      Y
 [1,]            


        
相关标签:
2条回答
  • 2020-12-11 21:01

    You can use ave with a custom function that wraps max, so you can remove NA values:

    Data$Y <- ave(Data$Y, Data$X, FUN=function(x) max(x, na.rm=TRUE))
    
    0 讨论(0)
  • 2020-12-11 21:08

    With the dplyr or data.table packages, you get an easy way group to calculate grouped operations.

    dplyr solution

    require(dplyr)
    Data %>% group_by(X) %>% mutate(Y = max(Y, na.rm=TRUE))
    

    data.table solution

    require(data.table)
    setDT(Data)[, Y:=max(Y, na.rm=TRUE), by=X][]
    
    0 讨论(0)
提交回复
热议问题