createGapFiller<- function(gaps=c(NA,'')){
currentVal <- NA
func <- function(y){
if (y %in% gaps) {
return(currentVal)
}
else {
currentVal <<- y
return(y)
}
}
return (function(v){
mapply(func,v)
})
}
myGapFiller<- createGapFiller(gaps=c(NA,''))
csvWithGaps <- read.csv('mydata.csv')
csvWithoutGaps <- data.frame(lapply(as.list(csvWithGaps),myGapFiller))
> csvWithGaps
Condition File Record Time
1 Control 44 1 1.3
2 NA 2 5.3
3 NA 3 6.7
4 NA 4 3.5
5 NA 5 3.7
6 NA 6 8.3
7 Experiment 46 1 5.7
8 NA 2 3.6
9 NA 3 8.6
10 NA 4 3.5
11 NA 5 6.6
12 NA 6 6.6
13 NA 7 0.0
> csvWithoutGaps
Condition File Record Time
1 Control 44 1 1.3
2 Control 44 2 5.3
3 Control 44 3 6.7
4 Control 44 4 3.5
5 Control 44 5 3.7
6 Control 44 6 8.3
7 Experiment 46 1 5.7
8 Experiment 46 2 3.6
9 Experiment 46 3 8.6
10 Experiment 46 4 3.5
11 Experiment 46 5 6.6
12 Experiment 46 6 6.6
13 Experiment 46 7 0.0