r - Difficultly filling NAs with imputed values by loop but not one-by-one -
so have matrix in r that's subset of dataframe holding little more id, grouping variable, , series of 1100 readings. i'm wanting run these readings through procedure requires spacing of values - each row (of approx 100) has 2 or 3 'missing values' day messing sensor various reasons. i'm wanting throw in loess imputation these values - 3-5% of each row generally, no more, i'm not terribly worried effect of few values other they're missing.
the code i'm using below. forgive inelegance in using loop lifting, , recognize yval not necessary i'm doing, makes easier track.
#take out matrix loess imputation df3mat <- as.matrix(cccsdf3[,c(3:1102)]) #generate list of time values loess generation timelist <- c(1:timevals) #use loess imputation fill values each row in df3mat matrix. (i in nrow(df3mat)){ #take each row @ time yval vector yval <- df3mat[i,] #get vector of values in yval missing missinglist <- which(is.na(yval)) #use span=0.13 loess curve, comes out 1 day of information in window fitting curve yval.loess <- loess(y ~ x, span=0.13, data.frame(x=timelist, y=yval)) #get set of predictions missing values yval.missing <- predict(yval.loess, data.frame(x=missinglist)) #replace missing values in yval yval.missing objects imputed via loess yval[is.na(yval)] <- yval.missing #and set row in df3mat filled yval vector. df3mat[i,] <- yval }
now, seem have happening this: when run code above, , view(df3mat), don't see changes @ in matrix final yval appears desired filled final row. however, if run 1 row (say, df3mat[6,]), works charm, , can see outcome in final df3mat. there problem in assignment? maybe hitting kind of memory problem in r?
you need fix for
loop. error here:
for(i in nrow(df3mat)) { ..... }
nrow(df3mat)
scalar value (single value) - not loop over. want like:
for(i in 1:nrow(df3mat)) { ..... }
or
for(i in seq_len(nrow(df3mat))) { ..... }
Comments
Post a Comment