Learn R Programming

trajr (version 1.5.1)

TrajsStatsReplaceNAs: Replace NAs in a data frame

Description

Replaces NAs in a single column of a data frame with an imputed uninformative numeric replacement value, so that a principal component analysis can be applied without discarding data. Optionally adds a new "flag" column which contains 1 for each row which originally contained NA, otherwise 0.

Usage

TrajsStatsReplaceNAs(
  df,
  column,
  replacementValue = mean(df[, column], na.rm = TRUE),
  flagColumn = NULL
)

Value

A copy of df with NAs replaced in column.

Arguments

df

Data frame to be adjusted.

column

Name or index of the column to be adjusted.

replacementValue

Numeric value to use instead of NA.

flagColumn

If not NULL, specifies the name of a new column to be added to the data frame, with value 0 for non-NA rows, 1 for NA rows. The column is added regardless of whether there are any NAs in the data.

See Also

Examples

Run this code
df <- data.frame(x = c(1, 2, 3), y = c(NA, 5, 6), z = c(NA, NA, 9))
# Eliminate NAs in y, add a flag column, ignore other NAs
df <- TrajsStatsReplaceNAs(df, "y", flagColumn = "y.was.NA")
print(df)

Run the code above in your browser using DataLab