Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

df <- data.frame(

  cola = c(3,NA,NA,NA,1,5),

  colb = c(7,NA,NA,NA,NA,6),

  colc = c(NA, NA, 10, NA, NA, 6)

)

From the above data frame, I want to have the max value of each row, and convert other value to NA, which means convert origin data.frame:

cola colb colc

   3    7   NA

  NA   NA   NA

  NA   NA   10

  NA   NA   NA

   1   NA   NA

   5    6   6

to expect result as below:

cola colb colc

NA    7   NA

NA   NA   NA

NA   NA   10

NA   NA   NA

1   NA   NA

NA    6   6

How to do it?

1 Answer

0 votes
by (107k points)

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.  

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...