na.as | R Documentation |
This function replaces NA
in a vector, factor, matrix or data frame with
user-specified values in the argument value
.
na.as(x, value, as.na = NULL, check = TRUE)
x |
a vector, factor, matrix or data frame. |
value |
a numeric value or character string with which |
as.na |
a numeric vector indicating user-defined missing values,
i.e. these values are converted to |
check |
logical: if |
Returns x
with NA
replaced with the numeric value or character
string specified in value
.
Takuya Yanagida takuya.yanagida@univie.ac.at
as.na
, na.auxiliary
, na.coverage
,
na.descript
, na.indicator
, na.pattern
,
na.prop
, na.test
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
#--------------------------------------
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.