如何在R中的数据帧中找到列的最大值? [英] How to find the highest value of a column in a data frame in R?

查看:1336
本文介绍了如何在R中的数据帧中找到列的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下称为臭氧的数据框:

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

我想从ozoneSolar.RWind ...

此外,如果可能的话,我将如何按降序对Solar.R或此数据框的任何列进行排序

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

我尝试了

max(ozone, na.rm=T)

这使我在数据集中获得了最高价值.

which gives me the highest value in the dataset.

我也尝试过

max(subset(ozone,Ozone))

但得到了"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) 

但是它给出了相同的值334,这是数据帧而不是列的最大值.

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.

推荐答案

类似于colMeanscolSums等,您可以编写列最大值函数colMax和列排序函数colSort .

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.

获取数据:

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")

对样本数据使用colMax功能:

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

要对单个列进行排序,

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

在所有列中都使用我们的colSort函数

and over all columns use our colSort function,

colSort(dat, decreasing = TRUE) ## compare with '...' above

这篇关于如何在R中的数据帧中找到列的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆