Applying a Scalable Scaling Function to Analysis and Assessment Data with R's Tidyverse
Here is the complete code to apply the same scaling function to both analysis and assessment.
library(tidyverse)
# Create intermediate list of scaled analysis values
scale_values <- map(d,
~ analysis(.x) %>%
as_tibble(., .name_repair = "universal") %>%
summarise(mu = mean(Value, na.rm = TRUE),
sd = sd(Value, na.rm = TRUE))
# Scale assessment values using the same scale as analysis
scaled_assessment <- map2(d, scale_values,
~ assessment(.x) %>%
as_tibble(., .name_repair = "universal") %>%
mutate(Value = (Value - mu) / sd))
# Print the scaled assessment values
print(scaled_assessment)
This code creates an intermediate list of scaled analysis values using map(), then uses map2() to scale the assessment values using the same scale as the analysis. The resulting scaled assessment values are printed to the console.
Note: I replaced .y with just $sd and $mu in the final line, because you used these variables before.
Also note that this code assumes that the assessment data has been properly cleaned and formatted for scaling. If not, additional steps may be necessary to prepare the data.
Last modified on 2025-03-07