Hi, For this example: O <- c(0 0 0 2 0 0 2 0) I want to create an array every time O[i] > 0. The array should be in the form; R[j] <- array(-1, dim=c(2,O[i])) i.e. if O[i] > 0 4 times I want 4 R arrays. Does anyone have any suggestions? Thanks, Doug -- View this message in context: http://r.789695.n4.nabble.com/Create-Arrays-tp2996706p2996706.html Sent from the R help mailing list archive at Nabble.com.
On Fri, Oct 15, 2010 at 9:55 AM, dpender <d.pender at civil.gla.ac.uk> wrote:> > Hi, > > For this example: > > O <- c(0 0 0 2 0 0 2 0) > > I want to create an array every time O[i] > 0. ?The array should be in the > form; > > R[j] <- array(-1, dim=c(2,O[i])) > > i.e. if O[i] > 0 4 times I want 4 R arrays. > > Does anyone have any suggestions? >Suggestion number l is don't use O for objects! Far too confusing! Serious suggestion is that a concrete example will help. Let me try: If: X = c(0,0,0,2,0,0,3,0) # I'm not using O here! Then R will be a list of length 2 because there are 2 values in X bigger than 0. R[1] will be array(-1,dim=c(2,2)) # because X[4] is 2 and R[2] will be array(-1,dim=c(2,3)) # because X[7] is 3 Yup? Okay, first get rid of the zeroes: Xnz = X[X!=0] That simplifies the problem. Then use lapply to iterate over Xnz with a function that returns the array given the value:> lapply(Xnz,function(x){array(-1,dim=c(2,x))})[[1]] [,1] [,2] [1,] -1 -1 [2,] -1 -1 [[2]] [,1] [,2] [,3] [1,] -1 -1 -1 [2,] -1 -1 -1 2-d arrays are just matrices, so you can do it all in one line with: lapply(X[X!=0],function(x){matrix(-1,2,x)}) Barry
Hi, Doug, maybe columns <- c( 0, 3, 0, 2, 0, 1) lapply( columns[ columns > 0], function( o) array( -1, dim = c( 2, o))) does what you want? Regards -- Gerrit --------------------------------------------------------------------- AOR Dr. Gerrit Eichner Mathematical Institute, Room 212 gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/~gcb7 --------------------------------------------------------------------- Zitat von dpender <d.pender at civil.gla.ac.uk>:> > Hi, > > For this example: > > O <- c(0 0 0 2 0 0 2 0) > > I want to create an array every time O[i] > 0. The array should be in the > form; > > R[j] <- array(-1, dim=c(2,O[i])) > > i.e. if O[i] > 0 4 times I want 4 R arrays. > > Does anyone have any suggestions? > > Thanks, > > Doug > -- > View this message in context: > http://r.789695.n4.nabble.com/Create-Arrays-tp2996706p2996706.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. >
Hi Gerrit, Almost it but I need to insert M[,i] as well as (matrix( -1, nrow( M), CN[i]) when CN[i] = 0 I know this is not correct but can something like the following be done? HH <- c(0.88, 0.72, 0.89, 0.93, 1.23, 0.86, 0.98, 0.85, 1.23) TT <- c(7.14, 7.14, 7.49, 8.14, 7.14, 7.32, 7.14, 7.14, 7.14) c <- c(0, 0, 0, 2, 0, 0, 0, 2, 0) TMP <- lapply( seq(c), function( i, CN, M) { if( CN[i] == 0) as.matrix( M[, i]) else (matrix( -1, nrow( M), CN[i]) && as.matrix( M[, i])) }, CN = c, M = rbind( HH, TT)) do.call( cbind, TMP) Doug -- View this message in context: http://r.789695.n4.nabble.com/Create-Arrays-tp2996706p2997060.html Sent from the R help mailing list archive at Nabble.com.