Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting Max Column Values in R

Tags:

r

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,]     1     10
 [2,]     1     12
 [3,]     1     NA
 [4,]     2     5
 [5,]     2     6
 [6,]     2     7
 [7,]     2     8
 [8,]     3     NA
 [9,]     3     NA
[10,]     3     1

I would like to select the max value of column Y and replace all values of Y within each group with that value. My output data frame would look like this:

          X      Y
 [1,]     1     12
 [2,]     1     12
 [3,]     1     12
 [4,]     2     8
 [5,]     2     8
 [6,]     2     8
 [7,]     2     8
 [8,]     3     1
 [9,]     3     1
[10,]     3     1

Any help would be appreciated. Thanks!

Here's the data

Data <- structure(list(X = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L), 
                     Y = c(10L, 12L, NA, 5L, 6L, 7L, 8L, NA, NA, 1L)), 
                .Names = c("X", "Y"), class = "data.frame",
                row.names = c("[1,]", "[2,]", "[3,]", "[4,]", "[5,]", "[6,]", "[7,]", "[8,]", "[9,]", "[10,]"))
like image 268
Matt Avatar asked Feb 19 '23 10:02

Matt


1 Answers

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))
like image 69
Joshua Ulrich Avatar answered Feb 22 '23 00:02

Joshua Ulrich