[R] Using indirect arguments in tapply

arun smartpink111 at yahoo.com
Tue Mar 4 19:01:11 CET 2014


HI,

I would use ?with() instead of ?attach.
sMean <- with(sorted,tapply(PRICE,ITEM,mean))
 sMean
#Double Single Triple 
# 352.4  379.9  563.5 
 
parameter <- data.frame(Pname,Iname,stringsAsFactors=FALSE)
 with(sorted,tapply(get(parameter[["Pname"]]),get(parameter[["Iname"]]),mean))
#Double Single Triple 
# 352.4  379.9  563.5 

A.K.


--------------------------------------------
On Tue, 3/4/14, Barry King <barry.king at qlx.com> wrote:

 Subject: [R] Using indirect arguments in tapply
 To: "r-help at r-project.org" <r-help at r-project.org>
 Date: Tuesday, March 4, 2014, 9:01 AM

 I am reading parameters from an Excel
 file but am having trouble using them
 in tapply.  Here is a mini-version of my problem.
 ------------------------------------------------------------------------------------------------------------------------------------------------
 sorted <-
 data.frame(c("Single","Single","Double","Double","Double",
 "Triple"),
                
      c(324.5, 435.3, 568.3, 345.4,
 143.5, 563.5))
 colnames(sorted) <- c("ITEM","PRICE")

 attach(sorted)
 sMean <- tapply(PRICE,ITEM,mean)  # works but cannot
 use column names
 directly
                
                
   # since reading them from an Excel file
 into
                
                
   # a parameter file

 Pname <- c("PRICE")
 Iname <- c("ITEM")
 parameter <- data.frame(Pname,Iname)  # This mimics
 reading the specific
 column names
                
                
       # from Excel
 # Now I want to indirectly reference the PRICE and ITEM
 columns but this
 does
 # not work as expected
 sMean2 <- tapply(parameter$Pname, parameter$Iname, mean)

 -- 

 Any  assistance would be greatly appreciated.
 __________________________
 *Barry E. King, Ph.D.*
 Chief Modeler
 Qualex Consulting Services, Inc.
 Barry.King at qlx.com
 O: (317)940-5464
 M: (317)507-0661
 __________________________

     [[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