I have a data frame with all numeric variables and one date variable. For each variable VARIABLE I want to create a dataframe using the following dplyr code:
avg_price = full_data_noNO %>%
group_by(Month, Country) %>%
dplyr::summarize(avg = mean(VARIABLE, na.rm = TRUE))
This works fine if I hard code the name VARIABLE but if I do it in a loop I get the warning In mean.default(data.matrix(VARIABLE), na.rm = TRUE) : argument is not numeric or logical: returning NA. As a result the average column in my avg_price dataframe only contains NA's. Does anyone know how to solve this problem?
Update: I currently have a function:
make_plots_expl_vars <- function (VARIABLE, full_data_noNO ) {
avg_price = full_data_noNO %>%
group_by(Month, Country) %>%
dplyr::summarize(avg = mean(VARIABLE, na.rm = TRUE))
return(avg_price)
Which I call using for example make_plots_expl_vars("price", full_data_noNO). I want to call this function for all variables in my dataframe using a loop, but I know how to do that.