r - Group data under conditions -
i working r on cross-section data , having problem when grouping data under conditions. problem can seen small part of huge database following. calculate average (distance) under conditions of same province, district , commune.
province district commune distance 101 15 3 15 101 15 3 5 101 15 3 7 101 15 9 1 101 15 9 7 102 18 19 3 102 18 19 10 103 16 22 5 103 16 22 6
the expected results following (divided each specific commune each district , each province):
province district commune distance 101 15 3 average 101 15 9 average 102 18 19 average 103 16 22 average
try
library(dplyr) df1 %>% group_by(province, district, commune) %>% summarise(distance=mean(distance))
or
aggregate(distance~., df1, mean)
or
library(data.table) setdt(df1)[, list(distance=mean(distance)), .(province, district, commune)]
Comments
Post a Comment