Re: [R] weighted average grouped by variables

2017-11-12 Thread PIKAL Petr
les, list(mydf$type), sum)$x gives you sums > > > > Cheers > > Petr > > > >> -----Original Message- > >> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of > >> Massimo Bressan > >> Sent: Thursday, November 9, 2017

Re: [R] weighted average grouped by variables

2017-11-11 Thread Berend Hasselman
erend Hasselman > aggregate(mydf$n_vehicles, list(mydf$type), sum)$x > gives you sums > > Cheers > Petr > >> -Original Message- >> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Massimo >> Bressan >> Sent: Thursday, November 9,

Re: [R] weighted average grouped by variables

2017-11-09 Thread Thierry Onkelinx
Dear Massimo, It seems straightforward to use weighted.mean() in a dplyr context library(dplyr) mydf %>% group_by(date_time, type) %>% summarise(vel = weighted.mean(speed, n_vehicles)) Best regards, ir. Thierry Onkelinx Statisticus / Statistician Vlaamse Overheid / Government of Flanders

Re: [R] weighted average grouped by variables

2017-11-09 Thread Massimo Bressan
hi thierry thanks for your reply yes, you are right, your solution is more straightforward best Da: "Thierry Onkelinx" A: "Massimo Bressan" Cc: "r-help" Inviato: Giovedì, 9 novembre 2017 15:17:31 Oggetto: Re: [R] weighted average grouped by variab

Re: [R] weighted average grouped by variables

2017-11-09 Thread PIKAL Petr
(mydf$type), sum)$x gives you sums Cheers Petr > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Massimo > Bressan > Sent: Thursday, November 9, 2017 2:17 PM > To: r-help > Subject: Re: [R] weighted average grouped by variables >

Re: [R] weighted average grouped by variables

2017-11-09 Thread Rui Barradas
Sorry, I messed up. Only checked the final result after sending the previous mail. The solution is wrong. Rui Barradas Em 09-11-2017 13:27, Rui Barradas escreveu: Hello, Using base R only, the following seems to do what you want. with(mydf, ave(speed, date_time, type, FUN = weighted.mean, w

Re: [R] weighted average grouped by variables

2017-11-09 Thread Rui Barradas
Hello, Using base R only, the following seems to do what you want. with(mydf, ave(speed, date_time, type, FUN = weighted.mean, w = n_vehicles)) Hope this helps, Rui Barradas Em 09-11-2017 13:16, Massimo Bressan escreveu: Hello an update about my question: I worked out the following solutio

Re: [R] weighted average grouped by variables

2017-11-09 Thread Massimo Bressan
Hello an update about my question: I worked out the following solution (with the package "dplyr") library(dplyr) mydf%>% mutate(speed_vehicles=n_vehicles*mydf$speed) %>% group_by(date_time,type) %>% summarise( sum_n_times_speed=sum(speed_vehicles), n_vehicles=sum(n_vehicles), vel=sum(s

[R] weighted average grouped by variables

2017-11-09 Thread Massimo Bressan
hi all I have this dataframe (created as a reproducible example) mydf<-structure(list(date_time = structure(c(1508238000, 1508238000, 1508238000, 1508238000, 1508238000, 1508238000, 1508238000), class = c("POSIXct", "POSIXt"), tzone = ""), direction = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L),