[R] applying cbind (or any function) across all components in a list

Hans Thompson hans.thompson1 at gmail.com
Fri May 25 00:37:51 CEST 2012


The function I am giving for context is cbind.  Are you asking how I would
like to apply the answer to my question?

I am trying to take the results of a Fluidigm SNP microarray, organized by
assay into a list (each component is the results of one assay), find
coordinate midpoints ([1,] and [2,] of my XX, XY, and YY clusters (these are
genotypes) and is represented by l1.  l2 is the midpoint between XX/XY and
XY/YY although I did not give this in my example for simplicity, and I am
now trying to find the midpoint between these new midpoints and their
closest genotype clusters. This is represented as

cbind((l1[[1]][,1]+l2[[1]][,1])/2, (l1[[1]][,2]+l2[[1]][,1])/2,
(l1[[1]][,2]+l2[[1]][,2])/2, (l1[[1]][,3]+l2[[1]][,2])/2)

but only works for one assay in the list of 96.  I want to apply this to the
entire list.  My entire code so far is:

## OPEN .CSV and ORGANIZE BY ASSAY


file=""
    {
    rawdata <- read.csv(file, skip = 15)
    OrgAssay <- split(rawdata, rawdata$Assay)


    ## RETURN MIDPOINTS FOR EACH CLUSTER WITHOUT NO CALLS

    #for loop
    ClustMidPts <-list()

    for(locus in 1:length(names(OrgAssay))){
    ClustMidPts[[locus]]<-t(cbind(tapply(OrgAssay[[locus]][,"Allele.X.1"],
OrgAssay[[locus]][,"Final"], mean,na.rm=T),
                               tapply(OrgAssay[[locus]][,"Allele.Y.1"],
OrgAssay[[locus]][,"Final"], mean,na.rm=T)))}

    names(ClustMidPts)=names(OrgAssay)


## CREATE CLUSTER-CLUSTER MIDPOINT

    #for loop
    ClustClustMidPts <- list()

    for(locus in 1:length(names(ClustMidPts))){
    ClustClustMidPts[[locus]] <-
cbind(XXYX=(ClustMidPts[[locus]][,"XX"]+ClustMidPts[[locus]][,"YX"])/2,
YXYY=(ClustMidPts[[locus]][,"YX"]+ClustMidPts[[locus]][,"YY"])/2)
}

    names(ClustClustMidPts)=names(ClustMidPts)


Please also let me know how I messed up the formatting because it shows up
fine in gmail even when I post on Nabble.  How did I assume you were using
Nabble?  Is this topic included in the posting guide?

--
View this message in context: http://r.789695.n4.nabble.com/applying-cbind-or-any-function-across-all-components-in-a-list-tp4631128p4631260.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list