Created
August 3, 2015 20:33
-
-
Save karawoo/34a74d834f21182c1117 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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