I am creating the following function:
mymean <- function(x){
xmean <- mean(x)
xmean <- as.data.frame(xmean)
xmean
}
When I use this function in the example below:
mymean(mtcars$mpg)
I get this result:
xmean
1 20.09062
How can I get the name of the actual variable (i.e. mpg) input by the user in the result above instead of xmean? Thanks.
CodePudding user response:
mymean <- function (x, xname=deparse(substitute(x))) {
xmean <- mean(x)
names(xmean) <- xname
xmean
}