An altruistic member here helped me write the following code to generate variables using a for loop and dplyr::summarize
. This code, as expected, works fine.
library(nycflights13)
flights <- nycflights13::flights %>%
select(carrier,distance,hour)
by_carrier <- NULL
for ( i in c("distance", "hour") {
df <-
flights %>%
dplyr::group_by(carrier) %>%
dplyr::summarize(!!as.name(i) := sum(!!as.name(i) ))
by_carrier <- bind_cols(by_carrier,df)
}
But when I change the for loop argument in the following manner, it encounters an error:
var_interest <- c("distance", "hour")
by_carrier <- NULL
for ( i in seq_along(var_interest)) {
df <-
flights %>%
dplyr::group_by(carrier) %>%
dplyr::summarize(!!as.name(i) := sum(!!as.name(i) ))
by_carrier <- bind_cols(by_carrier,df)
}
The error is as follows:
Error: Problem with `summarise()` input `1`.
x object '1' not found
i Input `1` is `sum(`1`)`.
i The error occurred in group 1: carrier = "9E".
Run `rlang::last_error()` to see where the error occurred.
What am I missing here? Thanks in advance.