Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

na.replace

Replace Missing Values


Description

Replace missing values

Usage

na.replace(x, replace, ...)

Arguments

x

vector possibly containing missing (NA) values

replace

either a scalar replacement value, or a function returning a scalar value

...

Optional arguments to be passed to replace

Details

This is a convenience function that is the same as x[is.na(x)] <- replace

Value

Vector with missing values (NA) replaced by the value of replace.

Author(s)

Gregory R. Warnes greg@warnes.net

See Also

Examples

x <- c(1,2,3,NA,6,7,8,NA,NA)
   
   # Replace with a specified value
   na.replace(x, '999')
   
   # Replace with the calculated median
   na.replace(x, median, na.rm=TRUE)

gtools

Various R Programming Tools

v3.8.2
GPL-2
Authors
Gregory R. Warnes, Ben Bolker, and Thomas Lumley
Initial release
2020-03-23

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.