Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find highest value in a data frame?

Tags:

dataframe

r

I have a dataframe x with this values:

   x1  x2  x3
1  NA   4   1
2  NA   3  NA
3   4  NA   2
4  NA   1  11
5  NA   2  NA
6   5  NA   1
7   5   9  NA
8  NA   2  NA

A simple question: How do I get the highest value? (11)

like image 261
R-obert Avatar asked Jun 12 '12 13:06

R-obert


2 Answers

you could write a column maximum function, colMax.

colMax <- function(data) sapply(data, max, na.rm = TRUE)

Use colMax function on sample data:

colMax(x)
#    x1     x2     x3
#   5.0    9.0    11.0   
like image 51
Alfonso Vergara Avatar answered Sep 28 '22 04:09

Alfonso Vergara


Use max() with the na.rm argument set to TRUE:

dat <- read.table(text="
   x1  x2  x3
1  NA   4   1
2  NA   3  NA
3   4  NA   2
4  NA   1  11
5  NA   2  NA
6   5  NA   1
7   5   9  NA
8  NA   2  NA", header=TRUE)

Get the maximum:

max(dat, na.rm=TRUE)
[1] 11
like image 35
Andrie Avatar answered Sep 28 '22 04:09

Andrie