[R] Using indirect arguments in tapply

Rui Barradas ruipbarradas at sapo.pt
Tue Mar 4 18:15:44 CET 2014


Hello,

Try instead


sMean2 <- tapply(parameter[[Pname]], parameter[[Iname]], mean)

Hope this helps,

Rui Barradas
Em 04-03-2014 17:01, Barry King escreveu:
> 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)
>




More information about the R-help mailing list