[R] the name of a variable in a function
Douglas Bates
bates at stat.wisc.edu
Wed Sep 17 20:54:16 CEST 2003
Well you don't know that the actual argument to a function is a name -
it could be a more complex expression. In any case, you can get the expression
that was the actual argument with the substitute function, then you
need to deparse it. Your function could be written
> prova <- function( var )
+ {
+ df <- data.frame(seq(min(var), max(var), len = 20))
+ names(df) <- deparse(substitute(var))
+ df
+ }
> prova(a)
a
1 1.000000
2 3.578947
3 6.157895
4 8.736842
5 11.315789
6 13.894737
7 16.473684
8 19.052632
9 21.631579
10 24.210526
11 26.789474
12 29.368421
13 31.947368
14 34.526316
15 37.105263
16 39.684211
17 42.263158
18 44.842105
19 47.421053
20 50.000000
"juli g. pausas" <juli at ceam.es> writes:
> Dear collegues,
> How can I get the name of a variable (and not the variable) within a
> function ?
>
> For instance, in the following function, I'd like to create a variable
> in the dataframe df with the same name to the variable passed in var:
>
>
>
> prova <- function( var )
> {
> df <- as.data.frame(matrix(nr=20,nc=0))
> df[["here"]] <- seq(min(var), max(var), le= 20) #
> df
> }
>
> for instance,
>
> a <- 1:50
> prova(a)
>
> should give a dataframe with a variable called a (df$a)
> Thanks in advance
>
> Juli
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
--
Douglas Bates bates at stat.wisc.edu
Statistics Department 608/262-2598
University of Wisconsin - Madison http://www.stat.wisc.edu/~bates/
More information about the R-help
mailing list