I'm trying to summarize data in a programmatic way across rows that I've created with another function and rlang.
This example has 2 traits and 3 materials, but I don't know how many there are in advance and I need to take these predictions and average them.
library(rlang)
library(dplyr)
set.seed(42)
df1 <- tibble(PLTID = 1:10, vigor_M1_pred = runif(10), vigor_M2_pred = runif(10), vigor_M3_pred = runif(10), senes_M1_pred = runif(10), senes_M2_pred = runif(10), senes_M3_pred = runif(10))
traits <- c("vigor", "senes")
mats <- c("M1", "M2", "M3")
# this gives me what I want, but I want to do it programmatically
df1 %>%
mutate(
vigor_avg = rowMeans(select(., starts_with("vigor"))),
senes_avg = rowMeans(select(., starts_with("senes")))
)
funs2 <- setNames(paste('rowMeans(select(., starts_with("', traits, '"), na.rm = TRUE)', sep = ""), paste0(traits, "_avg"))
#can't parse this
df1 %>% mutate(., !!!rlang::parse_exprs(funs2))
#Error in parse(text = x) : <text>:1:55: unexpected ';'
#1: rowMeans(select(., starts_with("vigor"), na.rm = TRUE);
I'd like to be able to generate the names and the function calls using the traits, but I can't seem to get it to happen.
I was pulling a lot from the discussion here:
Jiho has non-quoted arguments though. I'm trying to paste together the function and then parse it.