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
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,
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
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
(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
>
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
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
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
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),
9 matches
Mail list logo