如何在R或Excel中执行条件平均

时间:2016-07-09 12:21:02

标签: r excel-formula

我有一个如下所示的大数据集:

Image    | Length | Angel
-------------------------------- 
DSC_001  | 233.22 |2.00
--------------------------------
DSC_001  | 24.897 |1.2
--------------------------------
DSC_001  | 28.55  |2.87
--------------------------------
DSC_002  | 23.76  |3.71
--------------------------------
DSC_002  | 34.21  |3.21
---------------------------------

我想为每组设置LengthAngles的平均值(DSC_001是一组,DSC_002是另一组,等等。)

我可以在Excel中手动执行此操作,但在大约4000个数据点时需要花费大量时间。

我想知道我是如何以更智能的方式在R或Excel中执行此操作的?

2 个答案:

答案 0 :(得分:2)

R中,我们可以使用dplyr

library(dplyr)
df1 %>%
   group_by(image) %>%
   summarise_each(funs(mean))

data.table

library(data.table)
setDT(df1)[, lapply(.SD, mean) , by = image]

或使用aggregate

中的base R
aggregate(.~image, df1, FUN = mean)

答案 1 :(得分:0)

In Excel:

  1. Make a new list with the unique values in the Image column as decribed here.
  2. Add the column names above your new list (not mandatory but important for clear presentation of the data).
  3. Use AVERAGEIF() to compute a conditioned average with the formula: =AVERAGEIF(A2:A10,E3,B2:B10) assuming A2:A10 is the column Image, B2:B10 is The column of the values to calculate their mean, and E3 is the cell where the Image to calculate its' mean is stored.

Here is a screenshot to clarify this: Using AVERAGEIF()

Hope it helps ;)