I am working on a R script visual that feeds from data in PBI. I have a slicer that is populating a column in a dataframe from a choice of other column headers. This slicer is populating the "letters" column with the column names a through k. I would like to use the value of letters to call a column in a summarize function call. This is because letters will change dynamically as the user changes the slicer. I put a comment where I would like to call this value of letters instead of manually calling a column name of dataset.
library(tidyverse)
filtered <-filter(dataset,ScrapRecipeID==SelectedRecipe)
filteredSort<-filtered[order(filtered$YearMonthIndex),]
summarizedRecipe<-filteredSort %>%
dplyr::group_by(MonthYear,YearMonthIndex) %>%
dplyr::summarize(letterMean=mean(a)) %>% # want to call the value of letters here
ungroup()
summarizedAll<-dataset %>%
dplyr::group_by(MonthYear,YearMonthIndex) %>%
dplyr::summarize(letterMean2=mean(a)) %>% # want to call the value of letters here
ungroup()
dput of dataset:
dput(head(dataset))
structure(list(a = c(0.00266666666666667, 0.00266666666666667,
0.00266666666666667, 0.00266666666666667, 0.00266666666666667,
0.00266666666666667), b = c(2e-04, 2e-04, 2e-04, 2e-04, 2e-04,
2e-04), c = c(0, 0, 0, 0, 0, 0), d = c(0.00916666666666667, 0.00916666666666667,
0.00916666666666667, 0.00916666666666667, 0.00916666666666667,
0.00916666666666667), e = c(0.148333333333333, 0.148333333333333,
0.148333333333333, 0.148333333333333, 0.148333333333333, 0.148333333333333
), f = c(0.316666666666667, 0.316666666666667, 0.316666666666667,
0.316666666666667, 0.316666666666667, 0.316666666666667), g = c(0.0333333333333333,
0.0333333333333333, 0.0333333333333333, 0.0333333333333333, 0.0333333333333333,
0.0333333333333333), h = c(0.135, 0.135, 0.135, 0.135, 0.135,
0.135), SelectedRecipe = c("Recipe 85D", "Recipe 85D", "Recipe 85D",
"Recipe 85D", "Recipe 85D", "Recipe 85D"), YearMonthIndex = c(24257L,
24257L, 24257L, 24257L, 24257L, 24257L), MonthYear = c("05-2021",
"05-2021", "05-2021", "05-2021", "05-2021", "05-2021"), Year = c(12126L,
12126L, 12126L, 12126L, 12126L, 12126L), ScrapRecipeID = c("",
"", "", "", "", ""), i = c(0.00193333333333333, 0.00193333333333333,
0.00193333333333333, 0.00193333333333333, 0.00193333333333333,
0.00193333333333333), J = c(0.00933333333333333, 0.00933333333333333,
0.00933333333333333, 0.00933333333333333, 0.00933333333333333,
0.00933333333333333), k = c(0.0396666666666667, 0.0396666666666667,
0.0396666666666667, 0.0396666666666667, 0.0396666666666667, 0.0396666666666667
), letters = c("a", "a", "a", "a", "a", "a")), row.names = c(NA,
6L), class = "data.frame")
You can find the unique values in your
letterscolumn and then get the intersection of the values a-k. You can then pass that vector toacross()in asummarize()to get the mean of those columns that appear in the letter columns.edit if the letter will always be one letter you can use find the unique value and then pass it to mean with the help of rlang: