-1

i have a data.frame with columns and rows. how could i replace NA values so that it would be the average of the first value before and after that cell in that column?

for example:

 1. 1 2  3 
 2. 4 NA 7
 3. 9 NA 8 
 4. 1 5  6

I need the first NA to be - (5+2)/2=3.5 and the second to be (3.5+5)/2=4.25

  • 1
    It's not really clear what your input data looks like. Are the numbers column vectors, row vectors? Do you have a `data.frame`? A `list`? A `matrix`? Under point 1, 2 and 4 you give three elements, while there are 4 elements under point 3. Please use `dput` to provide data in a reproducible and unambiguous format. – Maurits Evers May 05 '19 at 09:19
  • What's the replacement logic? Can it be generalised for(to?) all rows? – NelsonGon May 05 '19 at 09:22
  • i edited it, is it better now? – ori fartook May 05 '19 at 09:42
  • @orifartook No not really. You didn't answer any of my questions. Do you have a `data.frame`? Or something else? You should edit your post to include the `dput` of your input sample data! If unclear review how to provide a [great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Maurits Evers May 05 '19 at 09:43
  • it's a data.frame, and i am not familiar with the dput function – ori fartook May 05 '19 at 09:46
  • @orifartook Read the details of the post about reproducible examples that I link to! As it stands the quality of your post is very poor. Please spend some time familiarising yourself with general posting guidelines here on SO. – Maurits Evers May 05 '19 at 11:52

1 Answers1

1

Lets create some sample data and transform it to data.table:

require(data.table)
require(zoo)
dat <- data.frame(a = c(1, 2, NA, 4)) 
setDT(dat)

Now, using the zoo::na.approx function we can impute the missing values.

dat[, newA:= na.approx(a, rule = 2)]

Output:

   a newA
1:  1    1
2:  2    2
3: NA    3
4:  4    4
DJV
  • 4,743
  • 3
  • 19
  • 34