0

just curious if there's a sneaky way to do this that i'm missing.

library(plyr)
library(data.table)

dfx <- data.frame(
  group = c(rep('A', 8), rep('B', 15), rep('C', 6)),
  sex = sample(c("M", "F"), size = 29, replace = TRUE),
  age = runif(n = 29, min = 18, max = 54) ,
  score = runif( n = 29 ) ,
  weight = sample( 0:1 , 29 , replace = TRUE )
)

dt_dfx <- as.data.table(dfx)

unweighted mean comparisons

# mean of all columns not specified in by=
dt_dfx[ , lapply( .SD , mean ) , by = .(sex,group) ]

# here's how to match the data.table unweighted mean
ddply(dfx, .(group,sex), numcolwise(mean))

not sure how to do this with plyr

# weighted.mean of all columns not specified in by=
dt_dfx[ , lapply( .SD , weighted.mean , weight ) , by = .(sex,group) ]

# easy way to match the data.table weighted.mean?

thanks all

eddi
  • 49,088
  • 6
  • 104
  • 155
Anthony Damico
  • 5,779
  • 7
  • 46
  • 77
  • protip: don't declare `dfx` as a `data.frame` -- just use `data.table` and it will be born into existence as a `data.table`. If you're starting with a `data.frame`, you can convert it to a `data.table` by reference (no copies) with `setDT(dfx)` (no need to reassign with `<-`) -- the conversion is done in-place. – MichaelChirico Oct 05 '16 at 19:11
  • 1
    since `plyr` is obsolete for these kinds of things - who cares? – eddi Oct 05 '16 at 19:13
  • I'm sensing an XY problem... – David Arenburg Oct 05 '16 at 19:14
  • @eddi `dplyr` then? what would the latest and greatest way to do this be? :) – Anthony Damico Oct 05 '16 at 19:33
  • 1
    @AnthonyDamico you already have the greatest way ;) as for the latest `plyr`-type way - sure, you [can do it](http://stackoverflow.com/questions/22644804/how-can-i-use-dplyr-to-apply-a-function-to-all-non-group-by-columns) in `dplyr` – eddi Oct 05 '16 at 19:37
  • @eddi that link doesn't solve the `weighted.mean` part, i don't think? – Anthony Damico Oct 05 '16 at 22:19

1 Answers1

2

Here is a dplyr solution, hope this helps

dfx %>%
group_by( sex , group ) %>%
summarize_each( funs( weighted.mean( . , weight ) ) , -weight )
david rae
  • 36
  • 4