I would like to map the following function over a list of columns from my dataframe in order to produce some descriptive statistics for each one of them. I would like to do it only with tidyverse tools.
Lets assume that we have a dataframe called "df", the variables to be summurised var1, var2 & var3. Then the following code is working:
a_function <- function(var, data) {
data %>%
summarise(
Var_Name = quo_name(var),
mean = mean({{ var }}, na.rm = TRUE),
sd = sd({{ var }}, na.rm = TRUE)
)
}
My_result<-
map_dfr(
.x = quos(var1, var2, var3 ),
.f = a_function ,
data = df
)
The problem starts when I am trying to pass a list of variables inside quos(). Bare in mind that I want the variable names to be passed as Var_Name characters strings in the "My_result" df
#The variable list to be passed on map
var_list <- df %>%
select(var1:var3) %>%
colnames()
My_result<-
map_dfr(
.x = quos(var_list ),
.f = a_function ,
data = df
)
The error is saying
Error in summarise():
! Problem while computing Var_Name = quo_name(var_list).
Caused by error in expr_name():
! expr must be a symbol, scalar, or call.
I am unable to generate the same error, but the alternative solution below, which removes the need for quos(), gets to the desired outcome of descriptive statistics for a list of columns .
Hi scottyd22, thanks for your reply. Your solution works on the example you gave me but when I try to switch to my actual problem I receive this
Error in is.data.frame(x) :
'list' object cannot be coerced to type 'double'
In addition: Warning message:
In mean.default(d, na.rm = T) :
argument is not numeric or logical: returning NA