[R] rownames as parameter in functions

Spencer Graves spencer.graves at pdf.com
Sun Feb 16 00:39:02 CET 2003


How about the following:

 > anArray <- array(1:4, dim=c(2,2))
 > dimnames(anArray) <- list(c("a", "b"), c("c", "d"))
 > anArray["a",]
c d
1 3
 >
You can then pass a vector of character strings to the function, and 
subset the rows  by "anArray[select.charvec,]".  Alternatively, 
"anArray[select.charvec,, drop=FALSE]" will ensure that the result is 
still a 2-dimensional array, not a vector [oops!!].

Does this help?
Spencer Graves

Johannes Schnitzler wrote:
> Dear all,
> 
> i want to give the names of columns of a data set (dat) as parameter to a
> function,
> in order to select single rows by the row names.
> 
> I tried several ways.
> Like:
> the data set :
> dat
> A  B C D  E
> 1  2  3  1  6
> 1  4  5  2  4
> ..  .. ..  ..  ..
> 
> the.col.names<-c("A","C")
> 
> f<-function(par){sapply,function(par)(dat$par*2)}
> 
> f(the.col.names)
> 
> Should give the result
> 
> A  C
> 2   6
> 2  10
> ..  ..
> 
> 
> Thank you in advance
> 
> 
> Johannes Schnitzler
> schnitzlerj at gmx.de
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list