X <- matrix(rnorm(100), ncol = 5) # Create a Matrix X
X[3, 5] <- NA # Replace a Single NA Inside
X[17, 2:4] <- c(NA, NA, NA) # Replace Three in a Row Inside
X[13:15, 4] <- c(NA, NA, NA) # Replace Three in a Column Inside
X[11:12, 5] <- c(NA, NA) # Replace Two at the Right Border
X[20, 1] <- NA # Replace One in the Lower Left Corner
X
Xts <- timeSeries(X) # convert X to timeSeries Xts
## remove rows with NAs
na.omit(Xts)
## Subsitute NA's with zeros or column means (formerly substituteNA())
na.omit(Xts, method = "z")
na.omit(Xts, FUN = "mean")
na.omit(Xts, FUN = "median")
## Subsitute NA's with a trimmed mean
na.omit(Xts, FUN = function(x, na.rm) mean(x, trim = 0.10, na.rm = na.rm))
## interpolate NA's linearily (formerly interpNA())
na.omit(X, method = "ir", interp = "linear")
na.omit(X, method = "iz", interp = "linear")
na.omit(X, method = "ie", interp = "linear")
## take previous values in a column
na.omit(X, method = "ir", interp = "before")
na.omit(X, method = "iz", interp = "before")
na.omit(X, method = "ie", interp = "before")
## examples with X (which is a matrix, not "timeSeries")
## (these examples are not run automatically as these functions are
## deprecated.)
if(FALSE){
## Remove Rows with NAs
removeNA(X)
## subsitute NA's by zeros or column means
substituteNA(X, type = "zeros")
substituteNA(X, type = "mean")
## interpolate NA's linearily
interpNA(X, method = "linear")
# Note the corner missing value cannot be interpolated!
## take previous values in a column
interpNA(X, method = "before")
# Also here, the corner value is excluded
}
Run the code above in your browser using DataLab