#--------------------------------------
# Numeric vector
x.num <- c(1, 3, NA, 4, 5)
# Replace NA with 2
na.as(x.num, value = 2)
#--------------------------------------
# Character vector
x.chr <- c("a", NA, "c", "d", "e")
# Replace NA with "b"
na.as(x.chr, value = "b")
#--------------------------------------
# Factor
x.factor <- factor(c("a", "a", NA, NA, "c", "c"))
# Replace NA with "b"
na.as(x.factor, value = "b")
#--------------------------------------
# Matrix
x.mat <- matrix(c(1, NA, 3, 4, 5, 6), ncol = 2)
# Replace NA with 2
na.as(x.mat, value = 2)
#--------------------------------------
# Data frame
x.df1 <- data.frame(x1 = c(NA, 2, 3),
x2 = c(2, NA, 3),
x3 = c(3, NA, 2), stringsAsFactors = FALSE)
# Replace NA with -99
na.as(x.df1, value = -99)
#--------------------------------------
# Recode value in data frame
x.df2 <- data.frame(x1 = c(1, 2, 30),
x2 = c(2, 1, 30),
x3 = c(30, 1, 2))
# Replace 30 with NA and then replace NA with 3
na.as(x.df2, value = 3, as.na = 30)
Run the code above in your browser using DataLab