Re: [R] Help with a function [along columns]

2014-10-14 Thread Jim Lemon
On Mon, 13 Oct 2014 08:54:51 PM Kate Ignatius wrote:
> Just an update to this:
> 
> gtal <- function(d) {
> alleles <- sapply(d, function(.) strsplit(as.character(.), "/"))
> gt <- unlist(lapply(alleles, function(x)
>ifelse(identical(x[[1]], vcf[,3]) & identical(x[[2]], vcf[,3]),
> 'RR', ifelse(identical(x[[1]], vcf[,4]) & identical(x[[2]], vcf[,4]), 'AA',
> ifelse(identical(x[[1]], vcf[,3]) & identical(x[[2]], vcf[,4]), 'RA',
> ifelse(identical(x[[1]], vcf[,4]) & identical(x[[2]],
> vcf[,3]), 'RA', ''))
> }
> 
> I've got something working but I'm having trouble with the gt part...
> I'm getting the error: object of type 'closure' is not subsettable.
> The vcf is my original file that I want to match with so not sure
> whether this a problem.
> 

Hi Kate,
Unless you have passed "vcf" to your function, it is unlikely to recognize 
it. As you are working with genome data, I suspect that there is a 
function named "vcf" somewhere in the parent environment and you 
can't subset a function.

Jim

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Help with a function [along columns]

2014-10-13 Thread Kate Ignatius
Just an update to this:

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

I've got something working but I'm having trouble with the gt part...
I'm getting the error: object of type 'closure' is not subsettable.
The vcf is my original file that I want to match with so not sure
whether this a problem.

On Mon, Oct 13, 2014 at 4:46 PM, Kate Ignatius  wrote:
> 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!

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Help with a function [along columns]

2014-10-13 Thread Kate Ignatius
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!

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.