Skip to content

Instantly share code, notes, and snippets.

@JohnArchieMckown
Created August 17, 2014 03:31
Show Gist options
  • Save JohnArchieMckown/6b20159dd8dd903f6778 to your computer and use it in GitHub Desktop.
Save JohnArchieMckown/6b20159dd8dd903f6778 to your computer and use it in GitHub Desktop.
Question from Kate Ignatius
---------------------------
Have:
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
But the goal is to have a file like this:
Family.ID Sample.ID Relationship PID MID
14 62 sibling 94 59
14 94 father 0 0
14 63 sibling 94 59
14 59 mother 0 0
17 6004 father 0 0
17 6003 mother 0 0
17 6005 sibling 6004 6003
17 368 sibling 6004 6003
130 202 mother 0 0
130 203 father 0 0
130 204 sibling 203 202
130 205 sibling 203 202
130 206 sibling 203 202
222 9 mother 0 0
222 45 sibling 18 9
222 34 sibling 18 9
222 10 sibling 18 9
222 11 sibling 18 9
222 18 father 0 0
====
Jorge I Velez answer (x contains family info above)
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
Solution assume a "nuclear" family with 0 or 1 fathers and 0 or 1 mothers. Won't work with a polygamous arrangement or a "merged" family of some sort.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment