[R] passing columns of tables as a variable to functions

Phil Spector spector at stat.berkeley.edu
Tue Mar 16 15:57:16 CET 2010


Sacha -
    The "$" notation can only be used when you literally use
the name of the column you want.  If the name of the column
is stored in a variable, you should use this notation:

       maptable[,value]

to refer to the column whose name is stored in "value".


 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Tue, 16 Mar 2010, Sacha Viquerat wrote:

> hello! ive come across the following problem:
> im writing a function to ease the production of maps. as my tables have 
> headers, id like to include an option in my function header to pass these 
> columns to use them inside the evaluation block. the function looks as 
> follows:
>
> # maptable #the name of the table containing coordinates and columns for 
> measured values
> # value: which column of my table to use (e.g. Alt, slope, us, etc...
> # labels(maptable)
> # [1] "pos.e" "pos.n" "Alt" "slope" "us" "uus" "lc"
>
> make.map<-function (maptable,value)
> {
> pos.e<-maptable$pos.e #calling the column directly works
> pos.n<-maptable$pos.n #calling the column directly works
> key<-maptable$value #this doesnt work. output: NULL
> ... rest of function, irrelevant to my question
> }
>
> for example, i call the function by:
>
> make.map(elevation,Alt,"Altitude","Sightings")
>
> and my interp from akima package denies work, since key is NULL
>
> interp(pos.e,pos.n,key,...)
>
> have i just missed something? ive browsed the help pages and the archive, 
> looked into crawleys R book but didnt find any solution, though im pretty 
> sure this should be achievable!
>
> ______________________________________________
> 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