Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the highest value of a column in a data frame in R?

Tags:

r

max

I have the following data frame which I called ozone:

   Ozone Solar.R Wind Temp Month Day 1     41     190  7.4   67     5   1 2     36     118  8.0   72     5   2 3     12     149 12.6   74     5   3 4     18     313 11.5   62     5   4 5     NA      NA 14.3   56     5   5 6     28      NA 14.9   66     5   6 7     23     299  8.6   65     5   7 8     19      99 13.8   59     5   8 9      8      19 20.1   61     5   9 

I would like to extract the highest value from ozone, Solar.R, Wind...

Also, if possible how would I sort Solar.R or any column of this data frame in descending order

I tried

max(ozone, na.rm=T) 

which gives me the highest value in the dataset.

I have also tried

max(subset(ozone,Ozone)) 

but got "subset" must be logical."

I can set an object to hold the subset of each column, by the following commands

ozone <- subset(ozone, Ozone >0) max(ozone,na.rm=T)  

but it gives the same value of 334, which is the max value of the data frame, not the column.

Any help would be great, thanks.

like image 827
Alfonso Vergara Avatar asked Jun 13 '14 19:06

Alfonso Vergara


People also ask

How do I find the highest value in a data set in R?

In R, we can find the minimum or maximum value of a vector or data frame. We use the min() and max() function to find minimum and maximum value respectively. The min() function returns the minimum value of a vector or data frame. The max() function returns the maximum value of a vector or data frame.

How do you find the maximum of a column?

To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.

How do I get the highest row value in R?

If we want to find the maximum of values two or more columns for each row in an R data frame then pmax function can be used.

How do I find the lowest value in a column in R?

To find the smallest number in an R data frame column excluding values zero or less, we can use min function along with subsetting of values greater than 0 through single square brackets.


2 Answers

Similar to colMeans, colSums, etc, you could write a column maximum function, colMax, and a column sort function, colSort.

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

I use ... in the second function in hopes of sparking your intrigue.

Get your data:

dat <- read.table(h=T, text = "Ozone Solar.R Wind Temp Month Day 1     41     190  7.4   67     5   1 2     36     118  8.0   72     5   2 3     12     149 12.6   74     5   3 4     18     313 11.5   62     5   4 5     NA      NA 14.3   56     5   5 6     28      NA 14.9   66     5   6 7     23     299  8.6   65     5   7 8     19      99 13.8   59     5   8 9      8      19 20.1   61     5   9") 

Use colMax function on sample data:

colMax(dat) #  Ozone Solar.R    Wind    Temp   Month     Day  #   41.0   313.0    20.1    74.0     5.0     9.0 

To do the sorting on a single column,

sort(dat$Solar.R, decreasing = TRUE) # [1] 313 299 190 149 118  99  19 

and over all columns use our colSort function,

colSort(dat, decreasing = TRUE) ## compare with '...' above 
like image 188
Rich Scriven Avatar answered Sep 21 '22 06:09

Rich Scriven


To get the max of any column you want something like:

max(ozone$Ozone, na.rm = TRUE) 

To get the max of all columns, you want:

apply(ozone, 2, function(x) max(x, na.rm = TRUE)) 

And to sort:

ozone[order(ozone$Solar.R),] 

Or to sort the other direction:

ozone[rev(order(ozone$Solar.R)),] 
like image 34
WheresTheAnyKey Avatar answered Sep 20 '22 06:09

WheresTheAnyKey