Home » excel » r – array averaging by parts

# r – array averaging by parts

Questions:

I have two columns of data. First contains measured values, second contains descriptive string. I want to calculate average values in segments where “desc” is not changing. For example:

``````values  desc    avg
1       aa       1
1       aa       1
1       bb       2
2       bb       2
2       bb       2
3       bb       2
3       cc     5.5
8       cc     5.5
8       aa     3.5
2       aa     3.5
2       aa     3.5
2       aa     3.5
``````

Column named “avg” is what I want to get. First and second row of avg contains mean value of the first two values (aa segment). Then follows mean value of bb segment (=2), then mean of cc segment and so on…

Is it possible in excel/vba or R?

Any help will be appreciated

mira

In Excel you can use this formula in C2 coped down

`=IF(B2=B1,C1,AVERAGE(A2:INDEX(A2:A\$1000,MATCH(TRUE,INDEX(B3:B\$1000<>B2,0),0))))`

assumes you have up to 1000 rows of data – adjust as required

`ave` in R base is great for this task

``````> df\$avg <- with(df, ave(values, desc, FUN=mean))
> df
values desc avg
1       1  aaa 1.0
2       1  aaa 1.0
3       1   bb 2.0
4       2   bb 2.0
5       2   bb 2.0
6       3   bb 2.0
7       3   cc 5.5
8       8   cc 5.5
9       8   aa 3.5
10      2   aa 3.5
11      2   aa 3.5
12      2   aa 3.5
``````

`data.table` is also awesome

``````> library(data.table)
> dt <- data.table(df)
> dt[, avg:=mean(values), by="desc"]
> dt
``````