ClaireS ClaireS - 1 month ago 14
R Question

Alternative to aggregate function that doesn't collapse df

I have person-level data and want to create a new variable that has the number of kids in a family. I have created a dummy variable for kids (1 if age<18, 0 otherwise). I'm currently using the aggregate function, where HH_ID is a household identifier.

No_kids <- aggregate(child ~ HH_ID, data = df, sum)


This code works but the data frame collapses whereas I want to assign the number of kids to each observation for that household. Is there an alternative to the aggregate function that doesn't collapse the data set?

989 989
Answer

Alternatively, you could do a merge after aggregation (so in base R):

ag <- aggregate(child ~ HH_ID, data = df, sum)
setNames(merge(df, ag, by="HH_ID"), c("HH_ID", "child", "No_kids"))
Comments