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)
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"))