R - calculating the average value of a dataframe column from the top row to bottom row -


the title may not clear, since difficult summarize problem in few words, although don't think problem difficult solve. explain problem, let me share dataframe reference:

head(df, n = 10)       team     score  1              10        2               4         3              10         4              16         5              20         6       b         5 7       b        11          8       b         8     9       b        16          10      b         5        

i'd add third column, calculates average score each team, average score updating go down rows each team, , resetting @ new team. example, output column hoping this:

head(df, n = 10)       team     score   avg_score 1              10          10  2               4           7 3              10           8 4              16          10 5              20          12 6       b         5           5 7       b        11           8  8       b         8           8 9       b        16          10  10      b         5           9   # row1: 10 = 10   # row2: 7 = (10 + 4)/2   # row3: 8 = (10 + 4 + 10)/3    # ...  

with pattern following, , calculation restarting new team.

thanks,

library("data.table") setdt(df)[, `:=` (avg_score = cumsum(score)/1:.n), = team] 

or more readable per comment @snoram

setdt(dt)[, avg_score := cumsum(score)/(1:.n), = team]  #    team score avg_score # 1:       10        10 # 2:        4         7 # 3:       10         8 # 4:       16        10 # 5:       20        12 # 6:    b     5         5 # 7:    b    11         8 # 8:    b     8         8 # 9:    b    16        10 # 10:    b     5         9 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -