如何在r中的数据框中找到数值的最大值和最小值?

时间:2021-07-13 20:11:58

I have a data frame with a few columns and need to calculate the difference between the average of a variable with respect to a factor. For example:

我有一个包含几列的数据框,需要计算变量相对于因子的平均值之间的差异。例如:

C1    C2 
3     A
5     B
9     A
10    A
12    A
9     B
20    B

And I want to get:

我想得到:

A 9
B 15

I tried

我试过了

aggregate(C1 ~ C2, data = df, FUN = function(x) max()-min()) 

but it didn't work. I have the feeling I should use sapply, but don't know how.

但它不起作用。我觉得我应该使用sapply,但不知道如何。

1 个解决方案

#1


1  

You have defined an anonymous function but have not specified what variables to use anywhere.

您已定义了匿名函数,但未指定要在任何位置使用的变量。

Replace function(x) max() - min() with function(x) max(x) - min(x).

用函数(x)max(x) - min(x)替换函数(x)max() - min()。

In other words:

换一种说法:

aggregate(C1 ~ C2, mydf, function(x) max(x) - min(x))
#   C2 C1
# 1  A  9
# 2  B 15

You could also conceivably use: aggregate(C1 ~ C2, mydf, function(x) diff(range(x))).

您还可以使用:aggregate(C1~C2,mydf,function(x)diff(range(x)))。

#1


1  

You have defined an anonymous function but have not specified what variables to use anywhere.

您已定义了匿名函数,但未指定要在任何位置使用的变量。

Replace function(x) max() - min() with function(x) max(x) - min(x).

用函数(x)max(x) - min(x)替换函数(x)max() - min()。

In other words:

换一种说法:

aggregate(C1 ~ C2, mydf, function(x) max(x) - min(x))
#   C2 C1
# 1  A  9
# 2  B 15

You could also conceivably use: aggregate(C1 ~ C2, mydf, function(x) diff(range(x))).

您还可以使用:aggregate(C1~C2,mydf,function(x)diff(range(x)))。