I have the following question: How to loop/ map the function below over all "X" variables cotained in the datframe instead of repetation (see below)? Further, I would like to name each vector according to the looped "X" variable (see below).
Thanks.
library("purrr")
library("tidyverse")
loadings <- data.frame(Met = c("Met1", "Met2", "Met3"),
Food = c("Fruit", "Nuts", "Eggs"),
X1 = c(0.5, 0.1, NA),
X2 = c(0.1, NA, 0.8),
X3 = c(0.3, 0.9, NA))
tbl_loadings <- function(data, depen_indepen_vars, pcs ) {
loadX <- data %>% select({{depen_indepen_vars}}, {{ pcs }} ) %>% filter({{ pcs }} != "NA") %>% as_tibble() %>% mutate(load = paste0({{depen_indepen_vars}}, " (", {{ pcs }} , "), ", collapse = "")) %>% select(load) %>% pull() %>% unique() %>% drop()
return(loadX)
}
# Instead of:
met_loadX1 <- tbl_loadings(loadings, Met, X1)
met_loadX2 <- tbl_loadings(loadings, Met, X2)
met_loadX3 <- tbl_loadings(loadings, Met, X3)
# map/loop over the data. Output names as above (met_loadX1, met_loadX2, met_loadX3):
Created on 2020-07-04 by the reprex package (v0.3.0)