[R] Help with functions - printing a variables name

Rui Barradas ruipbarradas at sapo.pt
Tue Oct 14 20:42:27 CEST 2014


Hello,

Maybe something like

data<-c(1,5,10)
func1<-function(x) {
     nm <- deparse(substitute(x))
     y<-x^2
     z<-x^3
     out<-data.frame(nm,y,z)
     return(out)
} #function

func1(data[1])

Hope this helps,

Rui Barradas

Em 14-10-2014 19:12, Evan Kransdorf escreveu:
> Hello Everyone,
>
> I was wondering if someone could help me implement a function in R.
>
> I want to pass a vector x to my function, peform some math, then output the
> data.  However, I want the output for x to be the *name of the vector
> I am *using
> as input.
>
> For example, data<-c(1,5,10)
>
>> func1<-function(x) {
>      y<-x^2
>      z<-x^3
>      out<-cbind(x,y,z)
>      return(out)
> } #function
>
> Desired output:
> data, 1, 1
> data, 25, 125
> data, 100, 1000
>
> Thanks very much for your help, Evan
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list