Tuesday, 15 September 2015

r - Combining an individual and aggregate level data sets -



r - Combining an individual and aggregate level data sets -

i've got 2 different info frames, lets phone call them "months" , "people".

months looks this:

month site x 1 1 4 2 1 3 3 1 5 1 2 10 2 2 7 3 2 5

and people looks this:

id month site 1 1 1 2 1 2 3 1 1 4 2 2 5 2 2 6 2 2 7 3 1 8 3 2

i'd combine them each time entry in "people" has particular month , site combination, it's added appropriate aggregated info frame, i'd following:

month site x people 1 1 4 2 2 1 3 0 3 1 5 1 1 2 10 1 2 2 7 3 3 2 5 1

but haven't foggiest thought of how go doing that. suggestions?

using base of operations packages

> aggregate( id ~ month + site, data=people, fun = length ) month site id 1 1 1 2 2 3 1 1 3 1 2 1 4 2 2 3 5 3 2 1 > res <- merge(months, aggdata, all.x = true) > res month site x id 1 1 1 4 2 2 1 2 10 1 3 2 1 3 na 4 2 2 7 3 5 3 1 5 1 6 3 2 5 1 > res[is.na(res)] <- 0 > res month site x id 1 1 1 4 2 2 1 2 10 1 3 2 1 3 0 4 2 2 7 3 5 3 1 5 1 6 3 2 5 1

r

No comments:

Post a Comment