I have a data with househould id's, gender and age as follows:
mydata <-
structure(list(ID_HH = c(1,1,1,1,2,2,3,3,3,4,5,5),
GENDER = c(1,2,1,1,1,2,2,1,2,2,1,1),
AGE = c(50,45,3,15,25,5,32,30,10,28,64,16)),
.Names = c("ID", "GENDER", "AGE"),
class = "data.frame", row.names = c(NA, -12L))
mydata
# HH_ID GENDER AGE
# 1 1 1 50
# 2 1 2 45
# 3 1 1 3
# 4 1 1 15
# 5 2 1 25
# 6 2 2 5
# 7 3 2 32
# 8 3 1 30
# 9 3 2 10
# 10 4 2 28
# 11 5 1 64
# 12 5 1 16
I have another dataframe, lets call it 'output', that has only the unique HH_ID values and some other columns next to it. What i would like to do is to add new columns to this data frame that shows:
- "the number of adult females (Gender=2 && Age=18)",
- "the number of adult males (Gender=1 && Age=18)",
- "the number of school children (6-18)" (Num_Sch), and
- "the number of preschpol children (0-6)"(Num_PreSch)
for each household. So 'output' should look like that:
# HH_ID Col1 Col2 ... Num_Fem Num_Male Num_PreSch Num_Sch
# 1 1 .. 1 1 1 1
# 2 2 .. 0 1 1 0
# 3 3 .. 1 1 0 1
# 4 4 .. 1 0 0 0
# 5 5 .. 0 1 0 1
I tried many different functions and packages but nothing could achieve exactly what I want. I would appreciate any help or comment.
Age >= 18
, notAge = 18
? – camille