Skip to content

Instantly share code, notes, and snippets.

@karawoo
Created August 3, 2015 20:33
Show Gist options
  • Save karawoo/34a74d834f21182c1117 to your computer and use it in GitHub Desktop.
Save karawoo/34a74d834f21182c1117 to your computer and use it in GitHub Desktop.
# In response to: https://twitter.com/duffy_ma/status/628300075110936576
df <- data.frame(x=1:10, y = 1:10)
library("plyr")
df$y <- mapvalues(df$y, from = c(5), to = c(NA)) # make sure to keep `y` column numeric
lengthdata <- length(df$x)
for(i in 1:(lengthdata-1)) {
if (is.na(df$y[i])) { # use is.na to test for NAs
df$y[i] <- ((df$y[i-1]+df$y[i+1])/2)
} else {
df$y[i] <- df$y[i]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment