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

David Winsemius dwinsemius at comcast.net
Fri May 25 02:32:25 CEST 2012


On May 24, 2012, at 6:37 PM, Hans Thompson wrote:

> 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 )

cbind would not be much different than c() in this case.

>
> but only works for one assay in the list of 96.

It's not really a function, but rather an expression that will be  
evaluated. If you had a function it might look like :

bindfn <- function(l1, l1) { c( 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 ) }

And you could perhaps (depending on whether l1 and l2 were  
constructed) properly execute:

do.call(bindfn, list(l1,l2))

perhaps:

mapply(bindfn, l1, l2)

You posted an example higher up in the thread but my wife is calling  
me to dinner. Will return later.

>  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?

Because you included no context. We cannot see your earlier postings.  
Expecting us to page out to Nabble or refer back to other item s in  
the thread is considered presumptuous on your part.

>  Is this topic included in the posting guide?
>
YES. It is. And you were asked to read the Posting Guide about 10  
times by now.

> --
> 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.
>
> ______________________________________________
> R-help at 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list