TrajsStatsReplaceNAs: Replace NAs in a data frame

View source: R/convenience.R

TrajsStatsReplaceNAsR Documentation

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
)

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.

Value

A copy of df with NAs replaced in column.

See Also

prcomp

Examples

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)


trajr documentation built on May 29, 2024, 11:52 a.m.