2

I need some help to solve the following problem :

I have a 47x42 data.frame with non null values in the upper-diag part, and I need to create a new data.frame based on this one where the value is defined by :

new_tab[i,j] <- 1/2 * previous_tab[i,j] + 1/2 * mean(adjacent_cells_of(i,j))

For example a smaller data.frame could be :

structure(list(X0 = c(10000L, 10000L, 10000L, 10000L, 10000L), 
X1 = c(2842L, 2842L, 2842L, 2842L, NA), X2 = c(1743L, 1743L, 
1743L, NA, NA), X3 = c(1144L, 1144L, NA, NA, NA), X4 = c(838L, 
NA, NA, NA, NA)), .Names = c("X0", "X1", "X2", "X3", "X4"
), row.names = 15:19, class = "data.frame")

      X0   X1   X2   X3  X4
15 10000 2842 1743 1144 838
16 10000 2842 1743 1144 838
17 10000 2842 1743   NA  NA
18 10000 2842   NA   NA  NA
19 10000   NA   NA   NA  NA

My problem is how can i define "mean of adjacent cells" giving that for [1,1] value there are only 3 adjacent cells and for [2,2] there are 8 values.

The new value of [2,2] should be :

1/2* 2842 +1/16*(10000+2842+1743+1743+1743+2842+10000+10000)

To put it in other words, it's a kind of smoothing values of my original data.frame by the next values. I don't know if i'm clear enough, but i can elaborate if needed.

Thanks for your help.

throwic
  • 153
  • 1
  • 8
  • 2
    You can check this link http://stackoverflow.com/questions/29105175/find-neighbouring-elements-of-a-matrix-in-r It might be useful – akrun Apr 02 '15 at 08:36
  • Thanks, it helped a lot. I couldn't find a thread on this subject. – throwic Apr 02 '15 at 08:44

1 Answers1

4

Most straightforward solution for finding the mean of adjacent values is good ol' for loop

m <- as.matrix(df)
x <- rbind(NA, cbind(NA, m, NA), NA)
z <- matrix(nrow=nrow(df), ncol=ncol(df))
for (i in 2:(nrow(x)-1)){
  for(j in 2:(ncol(x)-1)){
    y <- x[(i-1):(i+1), (j-1):(j+1)]
    y[2,2] <- NA
    z[i-1, j-1]= 1/2 * x[i, j] + 1/2 * mean(y, na.rm=TRUE)
  }
}
z
       [,1]     [,2]     [,3]   [,4] [,5]
[1,] 7614.0 4053.800 1843.000 1255.5  991
[2,] 7852.6 3978.062 1892.929 1293.1   NA
[3,] 7852.6 4218.857 2012.800     NA   NA
[4,] 8210.5 4879.500       NA     NA  NaN
[5,] 8210.5       NA       NA    NaN  NaN
ExperimenteR
  • 4,453
  • 1
  • 15
  • 19
  • 2
    this solution does not use the formula `new_tab[i,j] <- 1/2 * previous_tab[i,j] + 1/2 * mean(adjacent_cells_of(i,j))` – Mamoun Benghezal Apr 02 '15 at 08:40
  • 2
    Yes, that is right, according to the OP, `1/2* 2842 +1/16*(10000+2842+1743+1743+1743+2842+10000+10000) #[1] 3978.062`. May be it is a minor fix – akrun Apr 02 '15 at 08:41
  • 1
    Thanks, akrun, @MamounBenghezal for clarification – ExperimenteR Apr 02 '15 at 08:56
  • 1
    @MamounBenghezal Thanks for edit. `mean()-x[i,j]` was still insufficient. So I replaced it by `sum`. Hope it achieves what throwic asks. – ExperimenteR Apr 02 '15 at 08:58