You can simply apply the loop over the rows (MARGIN = 1) and substitute the values that are not equal to the max with NA, then assign the transpose back to the original object, like the following:
df[] <- t(apply(df, 1, function(x) replace(x, x != max(x, na.rm = TRUE), NA)))
Or you can do the same thing with rowMaxs
library(matrixStats)
i1 <- !!rowSums(!is.na(df))
df[i1,] <- replace(df[i1,], df[i1,] != rowMaxs(as.matrix(df[i1,]),
na.rm = TRUE)[col(df[i1,])], NA)
Or you can also use the dplyr package:
library(dplyr)
library(purrr)
df %>%
mutate(new = reduce(., pmax, na.rm = TRUE)) %>%
transmute_at(vars(starts_with('col')), ~ replace(., .!= new, NA))
If you are a beginner and want to know more about R then do check out the R programming course.