J_Chen
J_Chen

Reputation: 25

How to show the maximum value in each row?

I was wondering how I could tell R to only show the maximum values in each rows.

For instance, I would like this table:

> data<- randu
> data[1:10,]


         x        y        z
1  0.000031 0.000183 0.000824
2  0.044495 0.155732 0.533939
3  0.822440 0.873416 0.838542
4  0.322291 0.648545 0.990648
5  0.393595 0.826873 0.418881
6  0.309097 0.926590 0.777664
7  0.826368 0.308540 0.413932
8  0.729424 0.741526 0.884338
9  0.317649 0.393468 0.501968
10 0.599793 0.846041 0.678107

to look like this:

          x        y        z
1                    0.000824
2                    0.533939
3           0.873416 
4                    0.990648
5           0.826873 
6           0.926590 
7  0.826368 
8                    0.884338
9                    0.501968
10          0.846041 

Upvotes: 1

Views: 67

Answers (1)

akrun
akrun

Reputation: 887851

We can use pmax to find the max value per row

do.call(pmax, df)

and use that to create a logical matrix and replace those values that are not the max in each row to NA

df[df != do.call(pmax, df)[row(df)]] <- NA

Or another option is apply with MARGIN specified as 1 to loop through the rows and use the same logic

df[] - t(apply(df, 1, FUN = function(x) replace(x, x!= max(x), NA)))
df
#        x        y        z
#1        NA       NA 0.000824
#2        NA       NA 0.533939
#3        NA 0.873416       NA
#4        NA       NA 0.990648
#5        NA 0.826873       NA
#6        NA 0.926590       NA
#7  0.826368       NA       NA
#8        NA       NA 0.884338
#9        NA       NA 0.501968
#10       NA 0.846041       NA

Upvotes: 6

Related Questions