最大值或最小值,取决于另一个变量 [英] Max or min depending on another variable
问题描述
我需要根据波高的方向来计算波高的最大值和最小值,也就是说,我有两个变量:
I need to calculate the max and min of the wave height according to the direction from which it comes, that is to say, I have two variables:
- Hs(波高)
- 方向(膨胀方向)
我需要知道方向在11.25和33.75度之间的波的最大波高.
And I need to know the maximum wave height for waves with a direction between 11.25 and 33.75 degrees.
现在,使用以下功能:
Max (Hs [Direction [11.25: 33.75]))
但是我不同意结果与现有数据.
But I do not agree the result with the data that I have.
推荐答案
假定您的数据框名为df
,变量名为Hs
和Direction
,则可以使用
Assume your dataframe is called df
, your variables are called Hs
and Direction
, you can use
max(df$Hs[df$Direction >= 11.25 & df$Direction <= 33.75])
以获得在Direction
的定义值范围内的所有Hs
值中的最大值.
to get the maximum of all Hs
values within the defined value range of Direction
.
如果您像我一样不喜欢分别定义间隔的上限和下限,则可以使用此整齐的函数(我发现
If you, like me, dislike the necessity to define both lower and upper bounds of the interval separately, you can use this neat function (which I found here):
in_interval <- function(x, interval){
stopifnot(length(interval) == 2L)
interval[1] < x & x < interval[2]
}
然后使用
max(df$Hs[in_interval(df$Direction, c(11.25, 33.75))])
这篇关于最大值或最小值,取决于另一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!