2

I want to calculate mean of reward for each gamble and create a new column based on that. for example, this is my data:

 gamble<-c(1,2,3,4,2,3,4,1)
 reward <- c(1,0.5,0.5,0.4,0.5,0.4,0.2,0.5)
 new<-data.frame(gamble, reward)

and the new column should look like this:

gamble reward newcolumn
   1    1.0      0.75
   2    0.5      0.50
   3    0.5      0.45
   4    0.4      0.30
   2    0.5      0.50
   3    0.4      0.45
   4    0.2      0.30
   1    0.5      0.75

thank you very much in advance... i am really confused...

missB
  • 59
  • 1
  • 5

2 Answers2

1

If you are dealing with a very big data base, and you're concerned really about time, then data.table approach is a very good option:

> library(data.table)
> DT <- data.table(new)
> DT[, newColumn:=mean(reward), by=gamble]
> DT
   gamble reward newColumn
1:      1    1.0      0.75
2:      2    0.5      0.50
3:      3    0.5      0.45
4:      4    0.4      0.30
5:      2    0.5      0.50
6:      3    0.4      0.45
7:      4    0.2      0.30
8:      1    0.5      0.75
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
0

This is a very basic question, and you can probably find many examples here on SO. In base R, you can use ave to get the output you're looking for.

> new$newColumn <- with(new, ave(reward, gamble, FUN = mean))
> new
  gamble reward newColumn
1      1    1.0      0.75
2      2    0.5      0.50
3      3    0.5      0.45
4      4    0.4      0.30
5      2    0.5      0.50
6      3    0.4      0.45
7      4    0.2      0.30
8      1    0.5      0.75

In this case, FUN = mean is optional since that is the default function used by ave; I've included it just so you can see that it is possible to add other aggregation functions.

Unlike many of the other aggregation functions (aggregate, tapply, and so on), the output of ave is the same length as its input.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485