search for: n_vehicl

Displaying 7 results from an estimated 7 matches for "n_vehicl".

Did you mean: n_vehicles
2017 Nov 09
4
weighted average grouped by variables
...ructure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L), .Label = c("car", "light_duty", "heavy_duty", "motorcycle"), class = "factor"), avg_speed = c(41.1029082774049, 40.3333333333333, 40.3157894736842, 36.0869565217391, 33.4065155807365, 37.6222222222222, 35.5), n_vehicles = c(447L, 24L, 19L, 23L, 706L, 45L, 26L)), .Names = c("date_time", "direction", "type", "speed", "n_vehicles"), row.names = c(NA, -7L), class = "data.frame") mydf and I need to get to this final result mydf_final<-structure(l...
2017 Nov 09
1
weighted average grouped by variables
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 solution (with the package "dplyr") > > library(dplyr) > > mydf%>% > mutate(speed_vehicles=n_vehicles*mydf$sp...
2017 Nov 09
0
weighted average grouped by variables
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(speed_vehicles)/sum(n_vehicles) ) In fact I was hoping to manage everything in a "one-go": i.e. without the need to create the "intermed...
2017 Nov 09
2
weighted average grouped by variables
Hi Thanks for working example. you could use split/ lapply approach, however it is probably not much better than dplyr method. sapply(split(mydf, mydf$type), function(speed, n_vehicles) sum(mydf$speed*mydf$n_vehicles)/sum(mydf$n_vehicles)) gives you averages aggregate(mydf$n_vehicles, list(mydf$type), sum)$x gives you sums Cheers Petr > -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Massimo > Bressan > Sent: Thursd...
2017 Nov 11
0
weighted average grouped by variables
> On 9 Nov 2017, at 14:58, PIKAL Petr <petr.pikal at precheza.cz> wrote: > > Hi > > Thanks for working example. > > you could use split/ lapply approach, however it is probably not much better than dplyr method. > > sapply(split(mydf, mydf$type), function(speed, n_vehicles) sum(mydf$speed*mydf$n_vehicles)/sum(mydf$n_vehicles)) > gives you averages > The result of this calculation is: car light_duty heavy_duty motorcycle 36.54109 36.54109 36.54109 36.54109 But this doesn't give the same result as the dplyr method which is: d...
2017 Nov 09
1
weighted average grouped by variables
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 INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance thierry.onkelinx at inbo.b...
2017 Nov 09
0
weighted average grouped by variables
...ject.org> Inviato: Gioved?, 9 novembre 2017 15:17:31 Oggetto: Re: [R] weighted average grouped by variables 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 INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance thierry.onkelinx at...