I have found a solution to my problem (replacing certain values in multiple columns) but the solution is computationally expensive and takes extremely long to run on my data set which contains 1.4 million rows of data.

I have data that has been interpolated from one time stamp to another. The implication of this is that the newly interpolated data now has some values which contain decimals and are not a true reflection of the value they should be.

For example, in the following data:

```
G4<- data.frame(aX = c(0.968750, 0.970703, 0.980469, 0.949219, 0.960938, 0.966797, 0.935547, 0.875000, 0.882812, 0.871094, 0.871094),
                 aY = c(0.0468750, 0.0468750, 0.0410156, 0.0332031, 0.0585938, 0.1152340, 0.0996094, 0.0820312, 0.0781250, 0.0742188, 0.0859375),
                 aZ = c(0.234375, 0.234375, 0.242188, 0.236328, 0.234375, 0.246094, 0.236328, 0.228516, 0.236328, 0.287109, 0.289062),
                 Time = c("12/7/2013 15:04:30.496", "12/7/2013 15:04:30.536", "12/7/2013 15:04:30.577", "12/7/2013 15:04:30.617", "12/7/2013 15:04:30.657", "12/7/2013 15:04:30.697", "12/7/2013 15:04:30.736", "12/7/2013 15:04:30.776", "12/7/2013 15:04:30.815", "12/7/2013 15:04:30.855", "12/7/2013 15:04:30.895"),
                 Position = c(6.00000, 6.00000, 6.00000, 6.00000, 6.00000, 4.05629, 3.00000, 3.00000, 3.00000, 3.00000, 3.00000),
                 PreyEvent = c(0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.647905, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000),
                 PreyEventDetail = c(0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 1.29581, 2.00000, 2.00000, 2.00000, 2.00000, 2.00000),
                 Capture = c(0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 1.94371, 3.00000, 3.00000, 3.00000, 3.00000, 3.00000),
                 LikelyPrey = c(0.00000, 0.00000, 0.00000, 0.00000, 0.00000,   1.29581, 2.00000, 2.00000, 2.00000, 2.00000, 2.00000),
                 Video = c( 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2))

```

You will see that for columns which contain classes of animal behavior, certain values that have arisen from the interpolation contain decimals and rather need to be matched to the closest whole integer in the row above or below.

The example code below, iterates over each row of interest and picks the necessary value that has the closest match, and then replaces it. 

```
temp.dat <- data.frame()
for(i in 1:nrow(G4)){

  print(i)
  t1 <- G4[i,]
  t1before <- G4[i-1,]
  t1after <- G4[i+1,]

  ##Position
  x1 <- c(t1before$Position,t1after$Position)
  replace.value.pos <- x1[which.min(abs(x1 - t1$Position))]
  t1$Position <- replace.value.pos

  ##PreyEvent
  x2 <- c(t1before$PreyEvent,t1after$PreyEvent)
  replace.value.pe <- x2[which.min(abs(x2 - t1$PreyEvent))]
  t1$PreyEvent <- replace.value.pe

  ##PreyEventDetail
  x3 <- c(t1before$PreyEventDetail,t1after$PreyEventDetail)
  replace.value.pdet <- x3[which.min(abs(x3 - t1$PreyEventDetail))]
  t1$PreyEventDetail <- replace.value.pdet

  ##Capture
  x4 <- c(t1before$Capture,t1after$Capture)
  replace.value.c <- x4[which.min(abs(x4 - t1$Capture))]
  t1$Capture <- replace.value.c

  ##LikelyPrey
  x5 <- c(t1before$LikelyPrey,t1after$LikelyPrey)
  replace.value.lp <- x5[which.min(abs(x5 - t1$LikelyPrey))]
  t1$LikelyPrey <- replace.value.lp

  ##Video
  x6 <- c(t1before$Video,t1after$Video)
  replace.value.vid <- x6[which.min(abs(x6 - t1$Video))]
  t1$Video <- replace.value.vid

  temp.dat <- rbind(temp.dat,t1)

}
## Compare new data frame with original
temp.dat
G4

```

However, this code is inefficient and I am struggling to find a faster alternative.

Any suggestions would be greatly appreciated!