Here is a reproducible example:
I will start by assigning the mtcars dataset to a variable called temp
.
temp = mtcars
If we try to reference a column in this df, it works as expected. Results in a 'double'.
typeof(temp[,'wt'])
'double'
Now perform a simple group_by
and mutate
from the dplyr
. Then ungroup
.
temp = temp %>% group_by(gear) %>% mutate(var.wt = var(wt))
temp = temp %>% ungroup()
The resulting column reference is not a double anymore but a list.
typeof(temp[,'wt'])
'list'
If I try to compute the mean
of the referenced column, it doesn't work and results in the following error.
mean(temp[,'wt'])
In mean.default(typeof(temp[, "wt"])) :
argument is not numeric or logical: returning NA
How do I perform the mean
with column reference after the dplyr functions?