Re: [R] create arrays
I can get around it by doing something like: as.matrix(rep(1,291))*row(as.matrix(rep(1,291)))/10+.9 I was just hoping for a simple command. Schatzi wrote: In Matlab, an array can be created from 1 - 30 using the command similar to R which is 1:30. Then, to make the array step by 0.1 the command is 1:0.1:30 which is 1, 1.1, 1.2,...,29.9,30. How can I do this in R? - In theory, practice and theory are the same. In practice, they are not - Albert Einstein -- View this message in context: http://r.789695.n4.nabble.com/create-arrays-tp3503988p3503998.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] create arrays
On Fri, May 06, 2011 at 12:11:30PM -0700, Schatzi wrote: In Matlab, an array can be created from 1 - 30 using the command similar to R which is 1:30. Then, to make the array step by 0.1 the command is 1:0.1:30 which is 1, 1.1, 1.2,...,29.9,30. How can I do this in R? ... This may well be a hack, but 10:300/10 seemed to do it for me. Peace, david -- David H. Wolfskill r...@catwhisker.org Depriving a girl or boy of an opportunity for education is evil. See http://www.catwhisker.org/~david/publickey.gpg for my public key. pgpGpFjB08hOL.pgp Description: PGP signature __ R-help@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.
Re: [R] create arrays
?seq -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Schatzi Sent: Friday, May 06, 2011 1:12 PM To: r-help@r-project.org Subject: [R] create arrays In Matlab, an array can be created from 1 - 30 using the command similar to R which is 1:30. Then, to make the array step by 0.1 the command is 1:0.1:30 which is 1, 1.1, 1.2,...,29.9,30. How can I do this in R? - In theory, practice and theory are the same. In practice, they are not - Albert Einstein -- View this message in context: http://r.789695.n4.nabble.com/create- arrays-tp3503988p3503988.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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.
Re: [R] create arrays
On Fri, May 6, 2011 at 12:11 PM, Schatzi adele_thomp...@cargill.com wrote: In Matlab, an array can be created from 1 - 30 using the command similar to R which is 1:30. Then, to make the array step by 0.1 the command is 1:0.1:30 which is 1, 1.1, 1.2,...,29.9,30. How can I do this in R? Hmm, in this case, I would do it slightly differently: seq(from = 1, to = 30, by = .1) Cheers, Josh - In theory, practice and theory are the same. In practice, they are not - Albert Einstein -- View this message in context: http://r.789695.n4.nabble.com/create-arrays-tp3503988p3503988.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ __ R-help@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.
Re: [R] create arrays
Beautiful. -Original Message- From: greg.s...@imail.org [mailto:greg.s...@imail.org] Sent: Friday, May 06, 2011 02:17 PM To: Thompson, Adele - adele_thomp...@cargill.com; r-help@r-project.org Subject: RE: [R] create arrays ?seq -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Schatzi Sent: Friday, May 06, 2011 1:12 PM To: r-help@r-project.org Subject: [R] create arrays In Matlab, an array can be created from 1 - 30 using the command similar to R which is 1:30. Then, to make the array step by 0.1 the command is 1:0.1:30 which is 1, 1.1, 1.2,...,29.9,30. How can I do this in R? - In theory, practice and theory are the same. In practice, they are not - Albert Einstein -- View this message in context: http://r.789695.n4.nabble.com/create- arrays-tp3503988p3503988.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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.
Re: [R] create arrays
Some good suggestions, just (as always) be aware of floating-point imprecision. See FAQ 7.31 s - seq(1,30,0.1) s[8] [1] 1.7 s[8] == 1.7 [1] FALSE Just trying to forestall future questions :-) Dan Daniel Nordlund Bothell, WA USA -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of adele_thomp...@cargill.com Sent: Friday, May 06, 2011 12:18 PM To: greg.s...@imail.org; r-help@r-project.org Subject: Re: [R] create arrays Beautiful. -Original Message- From: greg.s...@imail.org [mailto:greg.s...@imail.org] Sent: Friday, May 06, 2011 02:17 PM To: Thompson, Adele - adele_thomp...@cargill.com; r-help@r-project.org Subject: RE: [R] create arrays ?seq -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Schatzi Sent: Friday, May 06, 2011 1:12 PM To: r-help@r-project.org Subject: [R] create arrays In Matlab, an array can be created from 1 - 30 using the command similar to R which is 1:30. Then, to make the array step by 0.1 the command is 1:0.1:30 which is 1, 1.1, 1.2,...,29.9,30. How can I do this in R? - In theory, practice and theory are the same. In practice, they are not - Albert Einstein -- View this message in context: http://r.789695.n4.nabble.com/create- arrays-tp3503988p3503988.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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. __ R-help@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.
Re: [R] Create Arrays
Hi, Doug, maybe 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) columnnumbers - c(0, 0, 0, 3, 0, 0, 0, 2, 0) TMP - lapply( seq( columnnumbers), function( i, CN, M) { if( CN[i] == 0) as.matrix( M[, i]) else matrix( -1, nrow( M), CN[i]) }, CN = columnnumbers, M = rbind( HH, TT)) do.call( cbind, TMP) gets close to what you want (after some adaptation, of course). HTH -- Gerrit - AOR Dr. Gerrit Eichner Mathematical Institute, Room 212 gerrit.eich...@math.uni-giessen.de Justus-Liebig-University Giessen Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany Fax: +49-(0)641-99-32109http://www.uni-giessen.de/cms/eichner __ R-help@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.
Re: [R] Create Arrays
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. __ R-help@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.
Re: [R] Create Arrays
On Fri, Oct 15, 2010 at 9:55 AM, dpender d.pen...@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 __ R-help@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.
Re: [R] Create Arrays
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.eich...@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.pen...@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@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. __ R-help@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.
Re: [R] Create Arrays
Barry, Gerrit, That was what I am after but unfortunately only the starting point. I am now trying to amend a function that inserts the R matrices into a dataset in the correct places: i.e. H - c(0.88, 0.72, 0.89, 0.93, 1.23, 0.86, 0.98, 0.85, 1.23) T - c(7.14, 7.14, 7.49, 8.14, 7.14, 7.32, 7.14, 7.14, 7.14) O - c(0, 0, 0, 3, 0, 0, 0, 2, 0) R - lapply(O[O!=0], function(x){matrix(-1,1,x)}) m - rbind(H, T) O2 - rbind(O,O) f - function(x, y, z) { if(nrow(x) != nrow(y) || nrow(y) != length(z)) stop('unequal numbers of rows among inputs') out - matrix(NA,2,length(H)+sum(O)) for(i in 1:2) out[[i,]] - append(x[i,], as.numeric(z[[i]]), after = which(y[i,] 0) - 1) out } f(m, O2, R) f is the function that requires amendment and was originally written for a single R value and treated each row of out separately. I now want to insert R[i] into m before every point that O[i] 0 Hope this makes sense. Doug I don -- View this message in context: http://r.789695.n4.nabble.com/Create-Arrays-tp2996706p2996776.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.