I have a data set(Bill) of with 1 variable (var1), with 100 obs that are in ascending order. I want to sample every 10 observations and save them in 10 different groups such as Group1 is obs 1-10 Group 2 is obs-11-20 and so on. First step is to subset them into the 10 groups, then calculate the mean of var1 for each of the 10 groups. Any help would be appreciated. Thanks. _________________________________________________________________ sh_skydrive_062008 [[alternative HTML version deleted]]
One possibility is: x <- ceiling((1:100)/10) aggregate(var1,by=list(x),mean) --- On Wed, 4/6/08, William Pepe <williampepe at hotmail.com> wrote:> From: William Pepe <williampepe at hotmail.com> > Subject: [R] Question about subsetting data > To: r-help at r-project.org > Received: Wednesday, 4 June, 2008, 10:35 AM > I have a data set(Bill) of with 1 variable (var1), with 100 > obs that are in ascending order. I want to sample every 10 > observations and save them in 10 different groups such as > > Group1 is obs 1-10 > Group 2 is obs-11-20 > > and so on. > > First step is to subset them into the 10 groups, then > calculate the mean of var1 for each of the 10 groups. Any > help would be appreciated. Thanks. > > _________________________________________________________________ > > > sh_skydrive_062008 > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Will this do it for you:> Bill <- 1:100 # test data > # partition > Bill.p <- split(Bill, rep(1:10, each=10)) > Bill.p$`1` [1] 1 2 3 4 5 6 7 8 9 10 $`2` [1] 11 12 13 14 15 16 17 18 19 20 $`3` [1] 21 22 23 24 25 26 27 28 29 30 $`4` [1] 31 32 33 34 35 36 37 38 39 40 $`5` [1] 41 42 43 44 45 46 47 48 49 50 $`6` [1] 51 52 53 54 55 56 57 58 59 60 $`7` [1] 61 62 63 64 65 66 67 68 69 70 $`8` [1] 71 72 73 74 75 76 77 78 79 80 $`9` [1] 81 82 83 84 85 86 87 88 89 90 $`10` [1] 91 92 93 94 95 96 97 98 99 100> sapply(Bill.p, mean)1 2 3 4 5 6 7 8 9 10 5.5 15.5 25.5 35.5 45.5 55.5 65.5 75.5 85.5 95.5> >On Tue, Jun 3, 2008 at 8:35 PM, William Pepe <williampepe@hotmail.com> wrote:> > I have a data set(Bill) of with 1 variable (var1), with 100 obs that are in > ascending order. I want to sample every 10 observations and save them in 10 > different groups such as > > Group1 is obs 1-10 > Group 2 is obs-11-20 > > and so on. > > First step is to subset them into the 10 groups, then calculate the mean of > var1 for each of the 10 groups. Any help would be appreciated. Thanks. > > _________________________________________________________________ > > > sh_skydrive_062008 > [[alternative HTML version deleted]] > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]