I want to apply the following normalization function in 'R':
normalize <- function(x) {return ((x - min(x)) / (max(x) - min(x)))}
However, my dataset has 68 observations and 960 variables, and some NaN here and there. Of course, before applying the function I would like to remove these NaNs
So, I used the following function:
df_normalized <- apply(df, 1, normalize, na.rm=TRUE)
However, the na.rm=TRUE
function is not being recognized.
CodePudding user response:
Two options -
- Change the
normalize
function to ignore theNA
values inmax
,min
functions.
normalize <- function(x) {
(x - min(x, na.rm = TRUE)) / (max(x, na.rm = TRUE) - min(x, na.rm = TRUE))
}
- Without changing the
normalize
function pass only non-NA values
df_normalized <- apply(df, 1, function(x) normalize(na.omit(x)))