Anyone who can help me with this? I have 48 observations (I dont want to alter their order). I want to group these observations into 16 blocks. So I should have 3 observations for each block. This is what I did in R, but it has warnings.> y #contains my 48 observations[1] 2.4 2.4 2.4 2.2 2.1 1.5 2.3 2.3 2.5 2.0 1.9 1.7 2.2 1.8 3.2 3.2 2.7 2.2 2.2 [20] 1.9 1.9 1.8 2.7 3.0 2.3 2.0 2.0 2.9 2.9 2.7 2.7 2.3 2.6 2.4 1.8 1.7 1.5 1.4 [39] 2.1 3.3 3.5 3.5 3.1 2.6 2.1 3.4 3.0 2.9> g=c(0,0,0) #storage vector > for (i in 1:16){+ g[i]=y[(3*i-2) : (3*i)] + } There were 16 warnings (use warnings() to see them)> g[1][1] 2.4> g[2][1] 2.2>###### g[1] should show 2.4 2.4 2.4 ###### g[2] should show 2.2 2.1 1.5 ######g[16] should show 3.4 3.0 2.9 Can you please tell me how I should correct my program? Thank you. ~tj -- View this message in context: http://n4.nabble.com/Create-blocks-or-observations-tp1691606p1691606.html Sent from the R help mailing list archive at Nabble.com.
tj wrote:> > Anyone who can help me with this? > I have 48 observations (I dont want to alter their order). I want to group > these observations into 16 blocks. So I should have 3 observations for > each block. This is what I did in R, but it has warnings. > >> y #contains my 48 observations > [1] 2.4 2.4 2.4 2.2 2.1 1.5 2.3 2.3 2.5 2.0 1.9 1.7 2.2 1.8 3.2 3.2 2.7 > 2.2 2.2 > [20] 1.9 1.9 1.8 2.7 3.0 2.3 2.0 2.0 2.9 2.9 2.7 2.7 2.3 2.6 2.4 1.8 1.7 > 1.5 1.4 > [39] 2.1 3.3 3.5 3.5 3.1 2.6 2.1 3.4 3.0 2.9 >> g=c(0,0,0) #storage vector >> for (i in 1:16){ > + g[i]=y[(3*i-2) : (3*i)] > + } > There were 16 warnings (use warnings() to see them) >> g[1] > [1] 2.4 >> g[2] > [1] 2.2 >> > > ###### g[1] should show 2.4 2.4 2.4 > ###### g[2] should show 2.2 2.1 1.5 > ######g[16] should show 3.4 3.0 2.9 > > Can you please tell me how I should correct my program? > Thank you. > > ~tj >I think your main problem is that you created a vector g[] which is three elements long. Then you tried to set a single element of g equal to three elements of y which doesn't work too well- you can't wedge a vector of length 3 into a box designed to hold one element. I would suggest paring y with a "group" vector that indicates which group each element of y belongs to: numGroups <- length( y ) / 3 group <- unlist( lapply( 1:numGroups, rep.int, times = 3 ) ) Then you can use the split function to break y apart based on group: split( y, group ) Or you could pair them as columns in a data frame and use split() to split into sub-data frames or by() to split and execute a function on each sub-data frame. Hope this helps! -Charlie ----- Charlie Sharpsteen Undergraduate-- Environmental Resources Engineering Humboldt State University -- View this message in context: http://n4.nabble.com/Create-blocks-or-observations-tp1691606p1691660.html Sent from the R help mailing list archive at Nabble.com.
On Mar 25, 2010, at 9:21 PM, tj wrote:> > Anyone who can help me with this? > I have 48 observations (I dont want to alter their order). I want to > group > these observations into 16 blocks. So I should have 3 observations > for each > block. This is what I did in R, but it has warnings. > >> y #contains my 48 observations > [1] 2.4 2.4 2.4 2.2 2.1 1.5 2.3 2.3 2.5 2.0 1.9 1.7 2.2 1.8 3.2 3.2 > 2.7 2.2 > 2.2 > [20] 1.9 1.9 1.8 2.7 3.0 2.3 2.0 2.0 2.9 2.9 2.7 2.7 2.3 2.6 2.4 1.8 > 1.7 1.5 > 1.4 > [39] 2.1 3.3 3.5 3.5 3.1 2.6 2.1 3.4 3.0 2.9Consider: > ygrp <- matrix(y, ncol=3, byrow=TRUE) > ygrp [,1] [,2] [,3] [1,] 2.4 2.4 2.4 [2,] 2.2 2.1 1.5 [3,] 2.3 2.3 2.5 [4,] 2.0 1.9 1.7 [5,] 2.2 1.8 3.2 [6,] 3.2 2.7 2.2 [7,] 2.2 1.9 1.9 [8,] 1.8 2.7 3.0 [9,] 2.3 2.0 2.0 [10,] 2.9 2.9 2.7 [11,] 2.7 2.3 2.6 [12,] 2.4 1.8 1.7 [13,] 1.5 1.4 2.1 [14,] 3.3 3.5 3.5 [15,] 3.1 2.6 2.1 [16,] 3.4 3.0 2.9 > ygrp[16, ] [1] 3.4 3.0 2.9 -- David.>> g=c(0,0,0) #storage vector >> for (i in 1:16){ > + g[i]=y[(3*i-2) : (3*i)] > + } > There were 16 warnings (use warnings() to see them) >> g[1] > [1] 2.4 >> g[2] > [1] 2.2 >> > > ###### g[1] should show 2.4 2.4 2.4 > ###### g[2] should show 2.2 2.1 1.5 > ######g[16] should show 3.4 3.0 2.9 > > Can you please tell me how I should correct my program? > Thank you. > > ~tj > > -- > View this message in context: http://n4.nabble.com/Create-blocks-or-observations-tp1691606p1691606.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.
[snip]> > group <- unlist( lapply( 1:numGroups, rep.int, times = 3 ) )This can be done simpler with: group <- rep(1:numgroups, each=3) -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111