Hi collective brain power. Here's the deal - I have a data frame that looks like this:
>df
Mom MomAge
aa 1
aa 3
aa 4
bb 2
bb 4
cc 1
cc 3
cc 5
Each row is a child born to Mom XX when she is XX years old. I would like to populate a new data frame that codes the existing data into binary responses (1 = child born at mom age X, 0 = no child born at mom age X) for each mom from ages 1 to 5:
1 2 3 4 5
aa 1 0 1 1 0
bb 0 1 0 1 0
cc 1 0 1 0 1
So far I have this:
>momlist<-unique(df$Mom)
>M<-matrix(data = NA, nrow = length(Mom), ncol=5)
>M<-data.frame(M)
>rownames(M) <-momlist
>colnames(M) <-c(1:5)
>M
1 2 3 4 5
aa NA NA NA NA NA
bb NA NA NA NA NA
cc NA NA NA NA NA
Not sure how to populate M...Maybe an if/then and then a for loop? Many thanks in advance!