Re: [R] data.table/ifelse conditional new variable question

2014-08-17 Thread Jorge I Velez
Dear Kate, Try this: res - do.call(rbind, lapply(xs, function(l){ l$PID - l$MID - 0 father - with(l, Relationship == 'father') mother - with(l, Relationship == 'mother') if(sum(father) == 0) l$PID[l$Relationship == 'sibling'] - 0 else l$PID[l$Relationship == 'sibling'] - l$Sample.ID[father]

Re: [R] data.table/ifelse conditional new variable question

2014-08-17 Thread Jorge I Velez
Perhaps I am missing something but I do not get the same result: x - read.table(textConnection(Family.ID Sample.ID Relationship 2702 349 mother 2702 3456 sibling 2702 9980 sibling 3064 3 father 3064 4 mother 3064 5sibling 3064 86 sibling 3064 87 sibling), header = TRUE)

Re: [R] data.table/ifelse conditional new variable question

2014-08-17 Thread Ted Harding
On 17-Aug-2014 03:50:33 John McKown wrote: On Sat, Aug 16, 2014 at 9:02 PM, Kate Ignatius kate.ignat...@gmail.com wrote: Actually - your code is not wrong... because this is a large file I went through the file to see if there was anything wrong with it - looks like there are two fathers or

Re: [R] data.table/ifelse conditional new variable question

2014-08-16 Thread Jorge I Velez
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

Re: [R] data.table/ifelse conditional new variable question

2014-08-16 Thread Kate Ignatius
Thanks! I think I know what is being done here but not sure how to fix the following error: Error in l$PID[l$\Relationship == sibling] - l$Sample.ID[father] : replacement has length zero On Sat, Aug 16, 2014 at 6:48 PM, Jorge I Velez jorgeivanve...@gmail.com wrote: Dear Kate, Assuming

Re: [R] data.table/ifelse conditional new variable question

2014-08-16 Thread Kate Ignatius
Actually - I didn't check this before, but these are not all nuclear families (as I assumed they were). That is, some don't have a father or don't have a mother Usually if this is the case PID or MID will become 0, respectively, for the child. How can the code be edit to account for this?

Re: [R] data.table/ifelse conditional new variable question

2014-08-16 Thread Kate Ignatius
Yep - you're right - missing parents are indicated as zero in the M/PID field. The above code worked with a few errors: 1: In l$PID[l$Relationship == sibling] - l$Sample.ID[father] : number of items to replace is not a multiple of replacement length 2: In l$PID[l$Relationship == sibling] -

Re: [R] data.table/ifelse conditional new variable question

2014-08-16 Thread Kate Ignatius
Actually - your code is not wrong... because this is a large file I went through the file to see if there was anything wrong with it - looks like there are two fathers or three mothers in some families. Taking these duplicates out fixed the problem. Sorry about the confusion! And thanks so much

Re: [R] data.table/ifelse conditional new variable question

2014-08-16 Thread John McKown
On Sat, Aug 16, 2014 at 9:02 PM, Kate Ignatius kate.ignat...@gmail.com wrote: Actually - your code is not wrong... because this is a large file I went through the file to see if there was anything wrong with it - looks like there are two fathers or three mothers in some families. Taking these