0

i have 2 df's ,in df1 we have NA values which needs to be replaced with mean of previous 2 years Average_f1 eg. in df1 - for row 5 year is 2015 and bin - 5 and we need to replace previous 2 years mean for same bin from df2 (2013&2014) and for row-7 we have only 1 year value

df1                  df2   
year    p1   bin       year bin_p1  Average_f1
2013    20    1        2013 5       29.5
2013    24    1        2014 5       16.5
2014    10   2         2015 NA      30
2014    11   2         2016 7       12  
2015    NA   5
2016    10   3
2017    NA   7

output
df1                 
year    p1   bin    
2013    20       1     
2013    24       1     
2014    10       2      
2014    11       2      
2015    **23**   5
2016    10       3
2017    **12**   7

Thanks in advance
d.b
  • 32,245
  • 6
  • 36
  • 77
Srm Murty
  • 135
  • 8
  • Please read the following post on posting a question [link1](https://www.r-bloggers.com/three-tips-for-posting-good-questions-to-r-help-and-stack-overflow/) and [link2](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Hanjo Odendaal Jun 30 '17 at 14:45
  • Your question is hardly understandable. Please, explain what you want to get more thoroughly. For example, how did you get 23 for the year 2015 in the output dataframe? – Alex Knorre Jul 01 '17 at 18:01

0 Answers0