0

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!

Jaap
  • 81,064
  • 34
  • 182
  • 193
PendaFisi
  • 77
  • 5

1 Answers1

1

You can pivot your data using dcast from reshape2 and provide a function to return 1 or 0 (instead of the default sum).

Setup Data

txt <- "Mom, MomAge 
aa,  1
aa,  3
aa,  4
bb,  2
bb,  4
cc,  1
cc,  3
cc,  5"

df <- read.csv(text = txt, header = TRUE)

Pivot Data

library(reshape2)
dcast(df, Mom ~ MomAge, fill=0, function(x){ x>0 })

  Mom 1 2 3 4 5
1  aa 1 0 1 1 0
2  bb 0 1 0 1 0
3  cc 1 0 1 0 1
Liesel
  • 2,929
  • 2
  • 12
  • 18