Choosing maximum column values ​​in R

I am new to R and asking a question about choosing maximum values ​​in a column.

I have the following data frame:

XY [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 maximum value of the Y column and replace all the Y values ​​within each group with this value. My output data frame will look like this:

  XY [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 greatly appreciated. Thanks!

Data here

 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,]")) 
+4
source share
2 answers

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

 Data$Y <- ave(Data$Y, Data$X, FUN=function(x) max(x, na.rm=TRUE)) 
+8
source

With dplyr or data.table you get a group of simple ways to compute 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
source

All Articles