[R] calculating mean for samples

Chuck Cleland ccleland at optonline.net
Tue Oct 21 15:11:56 CEST 2008


On 10/21/2008 8:41 AM, stephen sefick wrote:
> Yeah, I realized that after the fact, and haven't been able to figure
> out how to get it to work  -  The replicate function looks promising,
> but i could not get it to work in the couple of minutes that i played
> around with it.  I will think about it-  if you figure it out send me
> along the solution.

  How about putting the data frames into a list:

A1 <- (structure(list(s1 = c(0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L),
                      s2 = c(0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L),
                      s3 = c(0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
                      s4 = c(1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L)),
                      .Names = c("s1", "s2", "s3", "s4"),
                      class = "data.frame", row.names = c(NA, -10L)))

A1.list <- vector("list", 10)

for(i in 1:10){A1.list[[i]] <- A1[sample(nrow(A1), 5),]}

t(sapply(A1.list, colMeans))
t(sapply(A1.list, sd))

summary(t(sapply(A1.list, colMeans)))
summary(t(sapply(A1.list, sd)))

> On Tue, Oct 21, 2008 at 8:37 AM, al ex <loyola9988 at yahoo.com> wrote:
>> Thanks for the reply Stephen,
>> but the samples it makes are all the same:
>> " f <- do.call(rbind , rep(A1[sample(nrow(A1), 5),], 5)) " if U print out
>> "f", you'll see they are all the same.
>>
>> --- On Mon, 10/20/08, stephen sefick <ssefick at gmail.com> wrote:
>>
>> From: stephen sefick <ssefick at gmail.com>
>> Subject: Re: [R] calculating mean for samples
>> To: loyola9988 at yahoo.com, "R Help" <r-help at r-project.org>
>> Date: Monday, October 20, 2008, 12:05 PM
>>
>> assuming that A1 is the data provided in the first post... it would be
>> helpful if you used dput() to copy into an email, so that we could
>> just copy the code and data right into a R session and be off and
>> running.
>>   I hope this helps.
>>
>> #I used dput() on the object A1
>> A1 <- (structure(list(s1 = c(0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L),
>>     s2 = c(0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L), s3 = c(0L,
>>     1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), s4 = c(1L, 0L, 0L, 0L,
>>     1L, 0L, 0L, 0L, 0L, 1L)), .Names = c("s1", "s2",
>> "s3", "s4"
>> ), class = "data.frame", row.names = c(NA, -10L)))
>>
>> f <- do.call(rbind , rep(A1[sample(nrow(A1), 5),], 5))
>> d <- apply(f, MARGIN=1, FUN=mean)
>> mean(d[names(d)==s1])
>> mean(d[names(d)==s2])
>> mean(d[names(d)==s3])
>> mean(d[names(d)==s4])
>>
>> On Mon, Oct 20, 2008 at 2:44 PM, al ex <loyola9988 at yahoo.com> wrote:
>>> I dont have a problem with sampling and calculating mean:
>>>
>>> for(i in 1:5){
>>>  res=(A1[sample(nrow(A1), 5),])
>>>  Avg=colMeans(res)
>>>  STD=sd(res)
>>>  print(res)
>>>  print(Avg)
>>>  print(STD)
>>>  }
>>>
>>> my problem is
>>  how to save the mean for each "S" in each sample
>> and calculate
>>> the grand mean
>>>
>>>
>>>
>>> --- On Mon, 10/20/08, stephen sefick <ssefick at gmail.com> wrote:
>>>
>>> From: stephen sefick <ssefick at gmail.com>
>>> Subject: Re: [R] calculating mean for samples
>>> To: "Alex99" <loyola9988 at yahoo.com>
>>> Date: Monday, October 20, 2008, 11:34 AM
>>>
>>> look at
>>> ?sample
>>> ?lapply
>>> ?mean
>>>
>>> On Mon, Oct 20, 2008 at 1:53 PM, Alex99 <loyola9988 at yahoo.com>
>> wrote:
>>>>
>>>  Hi everyone,
>>>>> does any one knows how can I calculate mean for different samples
>>>>> i.e. I have a data like this:
>>>>>
>>>>>  s1 s2 s3 s4
>>>>> 1   0  0  0  1
>>>>> 2   1  0  1  0
>>>>> 3   0  0  0  0
>>>>> 4   0  0  0  0
>>>>> 5   0  1  0  1
>>>>> 6   1
>>   0  0  0
>>>>> 7   0  0  0  0
>>>>> 8   0  0  0  0
>>>>> 9   0  0  0  0
>>>>> 10  0  0  0  1
>>>>>
>>>>> I need to make 5 different sample with 5 different persons(rows)
>> in
>>> each
>>>>> sample from it keeping s1,s3,s3,s4 but changing rows. and then
>>> calculate
>>>>> the mean for each "S" in each sample. and finally
>> calculate
>>> the grand
>>>>> mean,which is the mean of means for each sample. i.e. if I sample
>> 5
>>> time I
>>>>> get 5 different means for s1, s2, s3, s4. then I need to add all
>> five
>>>>> means for and divide it by 5.(of course I have to do it
>>>  for
>>> s1,s2,s3,s4
>>>>> separately)
>>>> --
>>>> View this message in context:
>> http://www.nabble.com/calculating-mean-for-samples-tp20075174p20075174.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.
>>>>
>>>
>>>
>>> --
>>> Stephen Sefick
>>> Research Scientist
>>> Southeastern Natural Sciences Academy
>>>
>>> Let's not spend our time and resources thinking about things that are
>>> so little or so large that all they really do for us is puff us up and
>>> make us feel like gods.  We are mammals, and have not exhausted the
>>> annoying little problems of being mammals.
>>>
>>> 								-K.
>>>  Mullis
>>>
>>> __________________________________________________
>>> Do You Yahoo!?
>>> Tired of spam? Yahoo! Mail
>>  has the best spam protection around
>>> http://mail.yahoo.com
>>
>>
>> --
>> Stephen Sefick
>> Research Scientist
>> Southeastern Natural Sciences Academy
>>
>> Let's not spend our time and resources thinking about things that are
>> so little or so large that all they really do for us is puff us up and
>> make us feel like gods.  We are mammals, and have not exhausted the
>> annoying little problems of being mammals.
>>
>> 								-K. Mullis 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list