The “replace_na” Function in R
Package: tidyr
Purpose: To replace missing values with specified values.
General Class: Data Reshaping
Required Argument(s):
data: The data frame containing the columns with missing values.
replace: A list of replacements, with column names as named list components and replacement values as values.
Notable Optional Arguments:
None.
Example (with Explanation):
# Load necessary packages
library(tidyr)
# Create a sample data frame with missing values
data <- data.frame(
ID = 1:5,
Value = c(10, NA, 30, NA, 50)
)
# Replace missing values in the 'Value' column with a specified value
result <- replace_na(data, list(Value = -99))
# Display the result
print(result)In this example, the replace_na function from the tidyr package is used to replace missing values in the ‘Value’ column of the sample data frame data with the value -99. After replacement, any missing values in the ‘Value’ column are substituted with -99. The result is a new data frame result containing the original data with missing values replaced.