[R] error using ddply to generate means

Dennis Murphy djmuser at gmail.com
Sat Oct 1 18:21:14 CEST 2011


Hi:

Here's the problem:

> str(fun3)
'data.frame':   4 obs. of  3 variables:
 $ sector    :'data.frame':     4 obs. of  1 variable:
  ..$ gics_sector_name: chr  "Financials" "Financials" "Materials" "Materials"
 $ bebitpcchg: num  -0.567 0.996 NA -42.759
 $ ticker    : chr  "UBSN VX Equity" "LLOY LN Equity" "AI FP Equity"
"AKE FP Equity"

Notice that fun3$sector is a data frame, not a variable. By leaving
fun3 intact, the summary is gotten with

ddply(fun3, .(sector$gics_sector_name), summarise,
avgbebitchg=mean(bebitpcchg,na.rm=TRUE))
  sector$gics_sector_name avgbebitchg
1              Financials   0.2142787
2               Materials -42.7587479

You might consider reframing fun3, pardon the pun.

HTH,
Dennis
On Sat, Oct 1, 2011 at 7:58 AM, Aidan Corcoran
<aidan.corcoran11 at gmail.com> wrote:
> Dear list,
>
> I encounter an error when I try to use ddply to generate means as follows:
>
> fun3<-structure(list(sector = structure(list(gics_sector_name = c("Financials",
> "Financials", "Materials", "Materials")), .Names = "gics_sector_name",
> row.names = structure(c("UBSN VX Equity",
> "LLOY LN Equity", "AI FP Equity", "AKE FP Equity"), .Dim = 4L), class
> = "data.frame"),
>   bebitpcchg = c(-0.567449058550428, 0.99600643852127, NA,
>   -42.7587478692081), ticker = c("UBSN VX Equity", "LLOY LN Equity",
>   "AI FP Equity", "AKE FP Equity")), .Names = c("sector", "bebitpcchg",
> "ticker"), row.names = c(12L, 24L, 36L, 48L), class = "data.frame")
>
> fun3
>
>  gics_sector_name  bebitpcchg         ticker
> 12       Financials  -0.5674491 UBSN VX Equity
> 24       Financials   0.9960064 LLOY LN Equity
> 36        Materials          NA   AI FP Equity
> 48        Materials -42.7587479  AKE FP Equity
>
>
> fun4<-ddply(fun3,c("sector"),summarise,avgbebitchg=mean(bebitpcchg,na.rm=TRUE))
>
> Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing =
> decreasing)) :
>  undefined columns selected
>
> This is a small sample of my data. I’m probably overlooking some
> problem in my syntax, but would be very grateful if someone could
> point it out.
>
> Thanks in advance,
> Aidan.
>
> sessionInfo()
>
> R version 2.13.0 (2011-04-13)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> locale:
> [1] LC_COLLATE=English_Ireland.1252  LC_CTYPE=English_Ireland.1252
> LC_MONETARY=English_Ireland.1252
> [4] LC_NUMERIC=C                     LC_TIME=English_Ireland.1252
>
> attached base packages:
> [1] grid      stats     graphics  grDevices utils     datasets
> methods   base
>
> other attached packages:
>  [1] plm_1.2-7          sandwich_2.2-7     MASS_7.3-12
> Formula_1.0-1      nlme_3.1-100
>  [6] bdsmatrix_1.0      RBloomberg_0.4-149 rJava_0.8-8
> gtools_2.6.2       gdata_2.8.2
> [11] ggplot2_0.8.9      proto_0.3-9.2      zoo_1.7-4
> reshape_0.8.4      plyr_1.6
>
> loaded via a namespace (and not attached):
> [1] lattice_0.19-23 tools_2.13.0
>
> ______________________________________________
> 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