Home > front end >  Extracting the user-written command line for a function
Extracting the user-written command line for a function

Time:12-07

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
}

(from https://stackoverflow.com/a/24310574/972791)

  • Related