I am trying to sum 1 or more columns of data given a list of columns that should comprise the summation.
For example:
set.seed(3550)
# Creates data frame
month <- seq.Date(from = as.Date("2012-09-01"), by = "month", length.out = 50)
a <- rpois(50, 5000)
b <- rpois(50, 3000)
c <- rpois(50, 500)
d <- rpois(50, 1000)
df <- data.frame(month, a, b, c, d)
# Creates list of vectors
mylist <- list(this = "this", that = "that", other = "other")
mylist$this <- c("a")
mylist$that <- c("a", "b")
mylist$other <- c("a", "c", "d")
I can get results that I want with the following code:
my_df <- df %>%
group_by(month) %>%
summarize(this = sum(!!!rlang::syms(mylist$this), na.rm = TRUE),
that = sum(!!!rlang::syms(mylist$that), na.rm = TRUE),
other = sum(!!!rlang::syms(mylist$other), na.rm = TRUE))
With the output being:
# A tibble: 50 x 4
month this that other
<date> <int> <int> <int>
1 2012-09-01 4958 7858 6480
2 2012-10-01 4969 7915 6497
3 2012-11-01 5012 7978 6483
4 2012-12-01 4982 7881 6460
5 2013-01-01 4838 7880 6346
6 2013-02-01 5090 8089 6589
7 2013-03-01 5013 8044 6582
8 2013-04-01 4947 7942 6388
9 2013-05-01 5065 8124 6506
10 2013-06-01 5020 8086 6521
# ... with 40 more rows
I run into problems trying to figure out how to dynamically create the number of summarized columns. I thought looping inside the summarize call might work, but it did not.
combine_iterations <- function(x, iter_list){
a <- rlang::syms(names(iter_list))
b <- x %>%
group_by(month) %>%
summarize(for (i in 1:length(a)){
a[[i]] = sum(!!!rlang::syms(iter_list[i]), na.rm = TRUE)
})
}
Output:
Error in lapply(.x, .f, ...) : object 'i' not found
Called from: lapply(.x, .f, ...)
You are making it a little over complicated; If you want to customize the summarization, you can use
group_by %>% do
and avoid therlang
quote/unquote issue:Or another option creating data frame with
purrr::map_df
indo
: