Note: The date format is DD.MM.
I have the following data frame containing stock price data:
df1 <- data.frame(date = c("01.01.", "02.01.", "03.01.", "04.01.", "05.01.", "06.01."),
A = c(102, 103, 107, NA, 120, 134),
B = c(94, NA, 95, 100, 93, 90),
C = c(55, 53, 50, 51, 48, 15))
In order to normalize these across time, I'm using the following formula: x-mean(x) / sd(x)
I have the following code to apply this function to all relevant columns:
df1 = df1 %>%
mutate(across(.cols = A:C,
.f = function(x){(x-mean(x))/sd(x)}
))
However, for those columns that have at least one NA, it returns the entire column as NA. I've tried adding:
df1 = df1 %>%
mutate(across(.cols = A:C,
.f = function(x){(x-mean(x))/sd(x)}
), na.rm = TRUE)
But the same thing still happens. I don't want to use na.omit because I do need all the data available. How can I successfully apply the above function/one similar to it?