Hi all, I have a 4 by 100 matrix. I wish to extract each column and make it into a 2 by 2 matrix. I also want to assign names for each 2X2 matrix. For example set.seed(2) a=matrix(rnorm(400),ncol=100) a1=matrix(a[,1],ncol=2) a2=matrix(a[,2],ncol=2) . . . a100=matrix(a[,100],ncol=2) Any simple ideas. Thanks. B -- View this message in context: http://r.789695.n4.nabble.com/Column-Extraction-from-matrix-tp4640465.html Sent from the R help mailing list archive at Nabble.com.
set.seed(2) a=matrix(rnorm(400),ncol=100) #use a list aList<-list() for(i in 1:dim(a)[2]){ aList[[i]]<-matrix(a[,i],ncol=2) } #get lots of variables for(i in 1:dim(a)[2]){ assign(paste("a",i,sep=""),matrix(a[,i],ncol=2)) } On 16.08.2012, at 08:07, bantex wrote:> Hi all, > > I have a 4 by 100 matrix. I wish to extract each column and make it into a 2 > by 2 matrix. > I also want to assign names for each 2X2 matrix. > For example > > set.seed(2) > a=matrix(rnorm(400),ncol=100) > a1=matrix(a[,1],ncol=2) > a2=matrix(a[,2],ncol=2) > . > . > . > a100=matrix(a[,100],ncol=2) > > Any simple ideas. Thanks. > > B > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Column-Extraction-from-matrix-tp4640465.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.
> -----Original Message----- > I have a 4 by 100 matrix. I wish to extract each column and > make it into a 2 by 2 matrix.#make a toy 4x100 matrix: m <- matrix(rnorm(400), ncol=100) #use lapply after on-the-fly conversion to a list-like object: a.list <- lapply(as.data.frame(m),function(x) matrix(x, ncol=2)) #a list of 4x4 matrices with default names V1...V100> I also want to assign names for each 2X2 matrix.names(a.list) <- paste("a", 1:length(a.list), sep="") #a list of 4x4 matrices with default names a1...a100 If you don't care too much about preserving m, set column names on m first, via colnames(m) <- paste("a", 1:dim(m)[2], sep="") a.list <- lapply(as.data.frame(m),function(x) matrix(x, ncol=2)) #now returns the column names set on m If you really want separate variables cluttering up your workspace you would need to wrap it into a loop like for(i in 1:100) assign(paste('a',i, sep=''), matrix(m[,i], ncol=2)) But I would advise against that unless there is very good reason: it is awkward to reference many objects, and if they are in a named (or unnamed) list you can nearly always use various forms of lapply, or loops, applied to the list instead. For example, to get the determinants of all these little matrices sapply(a.list, det) works much more simply than some awkward loop code that constructs the variable names, uses get to reference them and then has to find somewhere to put all the determinants. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
Thanks guys for the help. I finally know what to do know :) -- View this message in context: http://r.789695.n4.nabble.com/Column-Extraction-from-matrix-tp4640465p4640482.html Sent from the R help mailing list archive at Nabble.com.
You could also use a 3d array instead of a list:> m <- matrix(1:400, ncol=100, byrow=TRUE) > a <- array(m, dim=c(2, 2, 100), dimnames=list(row=c(1, 2),col=c(1, 2), tbl=c(paste0("a", 1:100))))> a[,,1] # First table by indexcol row 1 2 1 1 201 2 101 301> a[,,"a1"] # First table by namecol row 1 2 1 1 201 2 101 301>> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of bantex > Sent: Thursday, August 16, 2012 7:28 AM > To: r-help at r-project.org > Subject: Re: [R] Column Extraction from matrix > > Thanks guys for the help. I finally know what to do know :) > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Column- > Extraction-from-matrix-tp4640465p4640482.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.