Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

R Dataframe Average Group by last months over Users

$
0
0

Suppose I have the next dataframe. How can I create a new "avg" column that is the result of averaging the last 2 dates ("date") for each group. The idea is to apply this to a dataset with hundreds of thousands of files, so performance is important. The function should contemplate a variable number of months (example 2 or 3 months) and be able to change between simple and medium average.

Thanks in advance.

table1<-data.frame(group=c(1,1,1,1,2,2,2,2),date=c(201903,201902,201901,201812,201903,201902,201901,201812),price=c(10,30,50,20,2,10,9,20))


  group   date price
1     1 201903    10
2     1 201902    30
3     1 201901    50
4     1 201812    20
5     2 201903     2
6     2 201902    10
7     2 201901     9
8     2 201812    20


result<-data.frame(group=c(1,1,1,1,2,2,2,2),date=c(201903,201902,201901,201812,201903,201902,201901,201812),price=c(10,30,50,20,2,10,9,20), avg = c(20, 40, 35, NA, 6, 9.5, 14.5, NA))


  group   date price  avg
1     1 201903    10 20.0
2     1 201902    30 40.0
3     1 201901    50 35.0
4     1 201812    20   NA
5     2 201903     2  6.0
6     2 201902    10  9.5
7     2 201901     9 14.5
8     2 201812    20   NA

Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>