So I have a matrix in R that's a subset of a dataframe holding little more than an ID, grouping variable, and a series of 1100 readings. I'm wanting to ultimately run these readings through a procedure that requires even spacing of the values - but each row (of approx 100) has two or three 'missing values' a day where we were messing with the sensor for various reasons. I'm wanting to just throw in a LOESS imputation for these values - which are about 3-5% of each row generally, no more, so I'm not terribly worried about the effect of those few values other than that they're missing.
The code I'm using for this is below. Forgive any inelegance in using a loop to do the lifting, and I recognize that yval is really not completely necessary for what I'm doing, but it makes it easier to track.
#Take out the matrix for LOESS imputation
df3mat <- as.matrix(cccsdf3[,c(3:1102)])
#Generate a list of the time values for the LOESS generation
timelist <- c(1:timevals)
#Use LOESS imputation to fill values for each row in the df3mat matrix.
for (i in nrow(df3mat)){
#Take each row at a time into the yval vector
yval <- df3mat[i,]
#Get a vector of which values in yval are missing
MissingList <- which(is.na(yval))
#Use span=0.13 for the LOESS curve, which comes out to about 1 day of information in the window for fitting the curve
yval.loess <- loess(y ~ x, span=0.13, data.frame(x=timelist, y=yval))
#Get a set of predictions just for the missing values
yval.missing <- predict(yval.loess, data.frame(x=MissingList))
#Replace the missing values in yval with the yval.missing objects as imputed via LOESS
yval[is.na(yval)] <- yval.missing
#And set the row in df3mat to the now filled yval vector.
df3mat[i,] <- yval
}
Now, what I seem to have happening is this: When I run the code above, and View(df3mat), I don't see any changes at all in the matrix but the final yval appears to be the desired filled final row. However, if I run it for just one row (say, df3mat[6,]), it works like a charm, and I can see the outcome in the final df3mat. Is there a problem in my assignment? Am I maybe hitting some kind of memory problem in R?