Skip to content
Prev 343195 / 398513 Next

data.table/ifelse conditional new variable question

Dear Kate,

Assuming you have nuclear families, one option would be:

x <- read.table(textConnection("Family.ID Sample.ID Relationship
14           62  sibling
14          94  father
14           63  sibling
14           59 mother
17         6004  father
17           6003 mother
17         6005   sibling
17         368   sibling
130           202 mother
130           203  father
130           204   sibling
130           205   sibling
130           206   sibling
222         9 mother
222         45  sibling
222         34  sibling
222         10  sibling
222         11  sibling
222         18  father"), header = TRUE)
closeAllConnections()

xs <- with(x, split(x, Family.ID))
res <- do.call(rbind, lapply(xs, function(l){
l$PID <- l$MID <- 0
 father <- with(l, Relationship == 'father')
 mother <- with(l, Relationship == 'mother')
 l$PID[l$Relationship == 'sibling'] <- l$Sample.ID[father]
 l$MID[l$Relationship == 'sibling'] <- l$Sample.ID[mother]
l
 }))
res

HTH,
Jorge.-


Best regards,
Jorge.-



On Sun, Aug 17, 2014 at 5:42 AM, Kate Ignatius <kate.ignatius at gmail.com>
wrote: