Search code examples
rmaxstat

R : select only the maximum value from each columns based on row in R


I just want to select the max value from each column, so the dimension of the result will be same as the amount of column.

predicted prob


Solution

  • you can use apply(MY_DATA, 2, max)

    for example with mtcars a default dataset build into R

    > apply(mtcars,2, max)
        mpg     cyl    disp      hp    drat      wt    qsec      vs      am    gear    carb 
     33.900   8.000 472.000 335.000   4.930   5.424  22.900   1.000   1.000   5.000   8.000 
    

    The apply function applies a function row-by-row (1) or column-by-column (2). Here I'm applying a function that returns the maximum on a column-by-column basis.