Hans Thompson
2012-May-23 19:58 UTC
[R] applying cbind (or any function) across all components in a list
#If I have two lists as follows a1<- array(1:6, dim=c(2,3)) a2<- array(7:12, dim=c(2,3)) l1<- list(a1,a2) a3<- array(1:4, dim=c(2,2)) a4<- array(5:8, dim=c(2,2)) l2<- list(a3,a4) #how can I create a new list with the mean across all arrays within the list, so all components are included? As an example for [[1]]; 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) -- View this message in context: http://r.789695.n4.nabble.com/applying-cbind-or-any-function-across-all-components-in-a-list-tp4631128.html Sent from the R help mailing list archive at Nabble.com.
Hans Thompson
2012-May-24 11:38 UTC
[R] applying cbind (or any function) across all components in a list
The combination of column means in cbind is what I am trying to do. I just don't know how to do it for every component (2 in this case) of the list. I've been able to work with R to apply a function across all components when there is just one list but I am having trouble working with multiple lists at the same time. -- View this message in context: http://r.789695.n4.nabble.com/applying-cbind-or-any-function-across-all-components-in-a-list-tp4631128p4631187.html Sent from the R help mailing list archive at Nabble.com.
David L Carlson
2012-May-25 14:28 UTC
[R] applying cbind (or any function) across all components in a list
This should give you what you want and it is simpler than the earlier version: a1<- array(1:6, dim=c(2,3)) a2<- array(7:12, dim=c(2,3)) l1<- list(a1,a2) a3<- array(1:4, dim=c(2,2)) a4<- array(5:8, dim=c(2,2)) l2<- list(a3,a4) pattern <- cbind(c(1, 2, 2, 3), c(1, 1, 2, 2)) lnew <- lapply(1:length(l1), function(i) (l1[[i]][,pattern[,1]]+l2[[i]][,pattern[,2]])/2) lnew If all the information from your several posts had been included in the original request, we could have responded more quickly. ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Hans Thompson > Sent: Thursday, May 24, 2012 5:19 PM > To: r-help at r-project.org > Subject: Re: [R] applying cbind (or any function) across all components > in a list > > Yes. This gives me: > > [,1] [,2] [,3] [,4] > [1,] 1 2 3 4 > [2,] 2 3 4 5 > [,1] [,2] [,3] [,4] > [1,] 6 7 8 9 > [2,] 7 8 9 10 > > BUT, how can I have it still within components like > > [[1]] > [,1] [,2] [,3] [,4] > [1,] 1 2 3 4 > [2,] 2 3 4 5 > > [[2]] > [,1] [,2] [,3] [,4] > [1,] 6 7 8 9 > [2,] 7 8 9 10 > > How should I have phrased my question to be specific to this result? > > Thanks. > > -- > View this message in context: http://r.789695.n4.nabble.com/applying- > cbind-or-any-function-across-all-components-in-a-list- > tp4631128p4631258.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.
Rui Barradas
2012-May-25 16:20 UTC
[R] applying cbind (or any function) across all components in a list
Hello, Let me give it a try. This last post made it clear, I hope. I have two interpretations of your problem. 1. 'l1' only has three columns, corresponding to clusters (genotypes) XX, XY and YY, and 'l2' has one less column, corresponding to the midpoints between their closest genotype cluster. 2. 'l1' can have any number of columns and 'l2' is the same as above, i.e., has one less column. In any case, the result is not the pairwise products of all possible combinations of columns of 'l1' and 'l2' matrices, but only those at a certain distance. In this case, fun2 below is more general. fun1 <- function(x, y){ cbind((x[, 1] + y[, 1])/2, (x[, 2] + y[, 1])/2, (x[, 2] + y[, 2])/2, (x[, 3] + y[, 2])/2) } fun2 <- function(x, y){ midpoint <- function(i, j) (x[, i] + y[, j])/2 colx <- ncol(x) res <- matrix(nrow = nrow(x), ncol = 2*colx - 2) k <- 1 res[, k] <- midpoint(1, 1) for(cx in seq_len(colx)[-c(1, colx)]) for(dist in 1:0) res[, k <- k + 1] <- midpoint(cx, cx - dist) res[, k + 1] <- midpoint(colx, colx - 1) res } lapply(seq_len(length(l1)), function(i) fun1(l1[[i]], l2[[i]])) lapply(seq_len(length(l1)), function(i) fun2(l1[[i]], l2[[i]])) If I'm wrong, sorry for the mess. Rui Barradas Em 25-05-2012 11:00, r-help-request at r-project.org escreveu:> Date: Thu, 24 May 2012 15:37:51 -0700 (PDT) > From: Hans Thompson<hans.thompson1 at gmail.com> > To:r-help at r-project.org > Subject: Re: [R] applying cbind (or any function) across all > components in a list > Message-ID:<1337899071674-4631260.post at n4.nabble.com> > Content-Type: text/plain; charset=us-ascii > > 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. > >