[R] Help with a function [along columns]

Kate Ignatius kate.ignatius at gmail.com
Mon Oct 13 22:46:12 CEST 2014


Hi all,

I need help with a function.  I'm trying to write a function to apply
to varying number of columns in a lot of files - hence the function...
but I'm getting stuck.  Here it is:

gt<- function(x) {
    alleles <- sapply(x, function(.) strsplit(as.character(.), "/"))
    gt <- apply(x, function(.) ifelse(x[1] == vcf[3] & x[2] == vcf[3], 'RR',
    ifelse(x[1] == vcf[4] & x[2] == vcf[4], 'AA',
    ifelse(x[1] == vcf[3] & x[2] == vcf[4], 'RA',
    ifelse(x[1] == vcf[4] & x[2] == vcf[3], 'RA', '')))))
}

I have different sized family genetic files and at the end of the day
I want to see whether the alleles of each person in the family match
the ref and/or the alt and if so, give AA, RA or RR.

Like so:

REF ALT Sample_1 GT_1 Sample_2 GT_2
A G A/A RR A/G RA
T G G/G AA T/T RR
A T T/T AA A/A RR
G A G/A RA G/G RR
G A G/G RR G/A RA
T C C/C AA C/C AA
T C C/C AA C/C AA
C T C/T RA T/T AA
G A A/A AA A/A AA
T G T/G RA G/G AA


Is there an easy way to do this?

Thanks!



More information about the R-help mailing list