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

How can i replace the the NULL values in dataframe with Average of Forward and backward fill? [duplicate]

$
0
0

I've just started using R and i'm trying to replace the NULl values in each columns by the average of forward and backward values. Below is the sample dataframe that has couple of null values

  A      B     C  
  1      1    2000
  2      NA   NA
  3      4    5000
  4      NA   NA
  5      7    8000

I'm trying to do something like this.

  A      B      C 
  1      1     2000
  2     2.5    3500
  3      4     5000
  4     5.5    6500
  5      7     8000

tidyr seems to do half of the work df %>% fill("A",.direction = c("down")) but how can i replace the average of the forward and the backward values ?


Viewing all articles
Browse latest Browse all 205491

Trending Articles



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