Re: [R] blockwise sums
Hi, all!! From the help page for 'aggregate': Splits the data into subsets, computes summary statistics for each, and returns the result in a convenient form. So here's the solution I found to this problem: blocksums - function(x,n) { temp - 1:length(x)-1 temp - list((temp%/%n)+1) aggregate(x,by=temp,sum)$x } For instance: blocksums(1:10,3) [1] 6 15 24 10 Hope this helps!! Vicente. __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] blockwise sums
If you insist, here's one way: my.blockwisesum - function(x, n, ...) { tapply(x, seq(1, length(x), by=n), sum, ...) } Andy From: Lutz Prechelt I am looking for a function like my.blockwisesum(vector, n) that computes sums of disjoint subsequences of length n from vector and can work with vector lengths that are not a multiple of n. It should give me for instance my.blockwisesum(1:10, 3) == c(6, 15, 24, 10) Is there a builtin function that can do this? One could do it by coercing the vector into a matrix of width n, and then use apply, but that is cumbersome if the length is not divisible by n, is it not? Any other ideas? Lutz Prof. Dr. Lutz Prechelt; [EMAIL PROTECTED] Institut fuer Informatik; Freie Universitaet Berlin Takustr. 9; 14195 Berlin; Germany +49 30 838 75115; http://www.inf.fu-berlin.de/inst/ag-se/ __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] blockwise sums
Liaw, Andy wrote: If you insist, here's one way: my.blockwisesum - function(x, n, ...) { tapply(x, seq(1, length(x), by=n), sum, ...) } Did you test that? I get: my.blockwisesum(1:10, 3) Error in tapply(x, seq(1, length(x), by = n), sum, ...) : arguments must have same length Here's my solution with tapply and rep() to generate a vector like c(1,1,1,2,2,2,3,3,3,4): baz.blockwisesum= function(v,n){tapply(v,rep(1:(1+length(v)/n),each=n)[1:length(v)],sum)} baz.blockwisesum(1:10,3) 1 2 3 4 6 15 24 10 - just ignore the 1 to 4 names, they cant hurt you. Baz __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] blockwise sums
Hi! ind-c(sort(rep(1:floor(length(x)/ 3 ) , 3 )) , floor(length(x)/ 3 )+1) by(x,ind,sum) my.blockwisesum-function(x,n,...) { ind-c(sort(rep(1:floor(length(x)/ n ) , n )) , floor(length(x)/ n )+1) return(tapply(x,ind,sum)) } /Eryk *** REPLY SEPARATOR *** On 8/31/2004 at 2:19 PM Lutz Prechelt wrote: I am looking for a function like my.blockwisesum(vector, n) that computes sums of disjoint subsequences of length n from vector and can work with vector lengths that are not a multiple of n. It should give me for instance my.blockwisesum(1:10, 3) == c(6, 15, 24, 10) Is there a builtin function that can do this? One could do it by coercing the vector into a matrix of width n, and then use apply, but that is cumbersome if the length is not divisible by n, is it not? Any other ideas? Lutz Prof. Dr. Lutz Prechelt; [EMAIL PROTECTED] Institut fuer Informatik; Freie Universitaet Berlin Takustr. 9; 14195 Berlin; Germany +49 30 838 75115; http://www.inf.fu-berlin.de/inst/ag-se/ __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Dipl. bio-chem. Witold Eryk Wolski @ MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin'v' tel: 0049-30-83875219/ \ mail: [EMAIL PROTECTED]---W-Whttp://www.molgen.mpg.de/~wolski [EMAIL PROTECTED] __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] blockwise sums
From: Barry Rowlingson Liaw, Andy wrote: If you insist, here's one way: my.blockwisesum - function(x, n, ...) { tapply(x, seq(1, length(x), by=n), sum, ...) } Did you test that? I get: Of course not (slap on wrist)!! My apologies... Andy my.blockwisesum(1:10, 3) Error in tapply(x, seq(1, length(x), by = n), sum, ...) : arguments must have same length Here's my solution with tapply and rep() to generate a vector like c(1,1,1,2,2,2,3,3,3,4): baz.blockwisesum= function(v,n){tapply(v,rep(1:(1+length(v)/n),each=n)[1:length( v)],sum)} baz.blockwisesum(1:10,3) 1 2 3 4 6 15 24 10 - just ignore the 1 to 4 names, they cant hurt you. Baz __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] blockwise sums
Hi Lutz, you could try the following: blockwisesum - function(x, n){ nx - length(x) if(nx%%n) x. - c(x, rep(0., n*ceiling(nx/n)-nx)) else x. - x x. - matrix(x., ncol=n, byrow=TRUE) rowSums(x.) } blockwisesum(1:10, 3) I hope this helps. Best, Dimitris Dimitris Rizopoulos Doctoral Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm - Original Message - From: Lutz Prechelt [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Tuesday, August 31, 2004 2:19 PM Subject: [R] blockwise sums I am looking for a function like my.blockwisesum(vector, n) that computes sums of disjoint subsequences of length n from vector and can work with vector lengths that are not a multiple of n. It should give me for instance my.blockwisesum(1:10, 3) == c(6, 15, 24, 10) Is there a builtin function that can do this? One could do it by coercing the vector into a matrix of width n, and then use apply, but that is cumbersome if the length is not divisible by n, is it not? Any other ideas? Lutz Prof. Dr. Lutz Prechelt; [EMAIL PROTECTED] Institut fuer Informatik; Freie Universitaet Berlin Takustr. 9; 14195 Berlin; Germany +49 30 838 75115; http://www.inf.fu-berlin.de/inst/ag-se/ __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] blockwise sums
Liaw, Andy wrote: If you insist, here's one way: my.blockwisesum - function(x, n, ...) { tapply(x, seq(1, length(x), by=n), sum, ...) } Did you test that? I get: my.blockwisesum(1:10, 3) Error in tapply(x, seq(1, length(x), by = n), sum, ...) : arguments must have same length Here's my solution with tapply and rep() to generate a vector like c(1,1,1,2,2,2,3,3,3,4): baz.blockwisesum= function(v,n){tapply(v,rep(1:(1+length(v)/n),each=n)[1:length( v)],sum)} baz.blockwisesum(1:10,3) 1 2 3 4 6 15 24 10 - just ignore the 1 to 4 names, they cant hurt you. Baz To complete the picture: here is another one: my.blockwisesum - function(vec, n){ vec - as.vector(vec) n - as.integer(n) total - length(vec) if(total = n){ stop(\nn should be smaller than length of vector.\n) } start - seq(1, total, n) end - start + n - 1 end[end total] - max(start) index - 1 : length(start) return(sapply(index, function(x)sum(test[start[x]:end[x]]))) } test - 1:150 ptn - proc.time() baz.blockwisesum(test,3) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 6 15 24 33 42 51 60 69 78 87 96 105 114 123 132 141 150 159 168 177 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 186 195 204 213 222 231 240 249 258 267 276 285 294 303 312 321 330 339 348 357 41 42 43 44 45 46 47 48 49 50 366 375 384 393 402 411 420 429 438 447 proc.time()-ptn [1] 0.00 0.00 0.22 NA NA ptn - proc.time() my.blockwisesum(test,3) [1] 6 15 24 33 42 51 60 69 78 87 96 105 114 123 132 141 150 159 168 [20] 177 186 195 204 213 222 231 240 249 258 267 276 285 294 303 312 321 330 339 [39] 348 357 366 375 384 393 402 411 420 429 438 447 proc.time()-ptn [1] 0.00 0.00 0.19 NA NA HTH, Bernhard The information contained herein is confidential and is inte...{{dropped}} __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] blockwise sums
Barry Rowlingson [EMAIL PROTECTED] writes: Liaw, Andy wrote: If you insist, here's one way: my.blockwisesum - function(x, n, ...) { tapply(x, seq(1, length(x), by=n), sum, ...) } Did you test that? I get: my.blockwisesum(1:10, 3) Error in tapply(x, seq(1, length(x), by = n), sum, ...) : arguments must have same length Here's my solution with tapply and rep() to generate a vector like c(1,1,1,2,2,2,3,3,3,4): baz.blockwisesum= function(v,n){tapply(v,rep(1:(1+length(v)/n),each=n)[1:length(v)],sum)} baz.blockwisesum(1:10,3) 1 2 3 4 6 15 24 10 Slight variant pd.blockwisesum - function(v,n){N - length(v); tapply(x,gl(ceiling(N/n), n, N), sum)} pd.blockwisesum(1:10,3) 1 2 3 4 6 15 24 10 -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] blockwise sums
Lutz Prechelt prechelt at pcpool.mi.fu-berlin.de writes: : : I am looking for a function like : my.blockwisesum(vector, n) : that computes sums of disjoint subsequences of length n from vector : and can work with vector lengths that are not a multiple of n. : : It should give me for instance : my.blockwisesum(1:10, 3) == c(6, 15, 24, 10) tapply(v, (seq(v)-1)%/%n, sum) __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] blockwise sums
On Tue, 31 Aug 2004, Liaw, Andy wrote: If you insist, here's one way: my.blockwisesum - function(x, n, ...) { tapply(x, seq(1, length(x), by=n), sum, ...) } If x is large, rowsum() should be faster than tapply(). -thomas Andy From: Lutz Prechelt I am looking for a function like my.blockwisesum(vector, n) that computes sums of disjoint subsequences of length n from vector and can work with vector lengths that are not a multiple of n. It should give me for instance my.blockwisesum(1:10, 3) == c(6, 15, 24, 10) Is there a builtin function that can do this? One could do it by coercing the vector into a matrix of width n, and then use apply, but that is cumbersome if the length is not divisible by n, is it not? Any other ideas? Lutz Prof. Dr. Lutz Prechelt; [EMAIL PROTECTED] Institut fuer Informatik; Freie Universitaet Berlin Takustr. 9; 14195 Berlin; Germany +49 30 838 75115; http://www.inf.fu-berlin.de/inst/ag-se/ __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Thomas Lumley Assoc. Professor, Biostatistics [EMAIL PROTECTED] University of Washington, Seattle __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] blockwise sums
This is a cute problem, so how about a cute algorithm? Since all that is wanted is the sum, matrix multiplication can be used. If n=vector length and b = blocksize, then the issue comes down to constructing a block diagonal matrix with nblk=n%/%b columns with b 1's in each block to multiply the first b*nblk elements of the vector by (the sum of any remaining elements can be appended to the result). This muliplier matrix is easily constructed using diag(): matrix(rep(diag(nblk),each=b),ncol=nblk) There may even be slicker ways to do it. Anyway, as no implicit looping (tapply) is used, I suspect this would run considerably faster,too, although this may be unimportant, and I haven't tested it. Of course, this approach doesn't generalize. But it is a nice example of some of R's little tricks. Cheers, -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA The business of the statistician is to catalyze the scientific learning process. - George E. P. Box -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Liaw, Andy Sent: Tuesday, August 31, 2004 5:42 AM To: 'Barry Rowlingson' Cc: [EMAIL PROTECTED] Subject: RE: [R] blockwise sums From: Barry Rowlingson Liaw, Andy wrote: If you insist, here's one way: my.blockwisesum - function(x, n, ...) { tapply(x, seq(1, length(x), by=n), sum, ...) } Did you test that? I get: Of course not (slap on wrist)!! My apologies... Andy my.blockwisesum(1:10, 3) Error in tapply(x, seq(1, length(x), by = n), sum, ...) : arguments must have same length Here's my solution with tapply and rep() to generate a vector like c(1,1,1,2,2,2,3,3,3,4): baz.blockwisesum= function(v,n){tapply(v,rep(1:(1+length(v)/n),each=n)[1:length( v)],sum)} baz.blockwisesum(1:10,3) 1 2 3 4 6 15 24 10 - just ignore the 1 to 4 names, they cant hurt you. Baz __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html