Mean of colMeans

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Mean of colMeans

Beejai
Hi All,

I've successfully gotten out the colMeans for 60 columns using:

col <- colMeans(x, na.rm = TRUE, dims = 1)

My next question is: is there a way of getting a mean of all the
column means (ie a mean of a mean)?

Thanks!

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Mean of colMeans

Igor Sosa Mayor
Kate Ignatius <[hidden email]> writes:
> Hi All,
>
> I've successfully gotten out the colMeans for 60 columns using:
>
> col <- colMeans(x, na.rm = TRUE, dims = 1)
>
> My next question is: is there a way of getting a mean of all the
> column means (ie a mean of a mean)?

maybe
mean(x)
?

--
:: Igor Sosa Mayor     :: [hidden email] ::
:: GnuPG: 0x1C1E2890   :: http://www.gnupg.org/      ::
:: jabberid: rogorido  ::                            ::

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Mean of colMeans

Sarah Goslee
In reply to this post by Beejai
That would be because col is a function in base R, and thus a poor
choice of names for user objects. Nonetheless, it worked when I ran
it, but you didn't provide reproducible example so who knows.

R> set.seed(1)
R> x <- data.frame(matrix(runif(150), ncol=10))
R> # col is a function, so not a good name
R> col <- colMeans(x)
R> mean(col)
[1] 0.5119

It's polite to include the list on your reply.

Sarah

On Wed, May 21, 2014 at 2:50 PM, Kate Ignatius <[hidden email]> wrote:

> That didn't work: gave me the error =
>
> [1] NA
> Warning message:
> In mean.default(col) : argument is not numeric or logical: returning NA
>
> But writing it like: mean(colMeans(x, na.rm = TRUE, dims = 1)), worked
>
> Thanks!
>
> On Wed, May 21, 2014 at 2:31 PM, Sarah Goslee <[hidden email]> wrote:
>> Is
>>
>> mean(col)
>>
>> not what you're looking for?
>>
>> Sarah
>>
>> On Wed, May 21, 2014 at 2:26 PM, Kate Ignatius <[hidden email]> wrote:
>>> Hi All,
>>>
>>> I've successfully gotten out the colMeans for 60 columns using:
>>>
>>> col <- colMeans(x, na.rm = TRUE, dims = 1)
>>>
>>> My next question is: is there a way of getting a mean of all the
>>> column means (ie a mean of a mean)?
>>>
>>> Thanks!
>>>

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Mean of colMeans

Beejai
Thanks for the explanation.  And tip... this was a quick a dirty code
so didn't really think about naming something that is already a
function in R.  Data was generic - just a bunch of columns with
numbers so didn't bother including that as I know that wasn't the
problem. Same goes with replying - automatically went to reply, will
remember to reply-all.

On Wed, May 21, 2014 at 3:11 PM, Sarah Goslee <[hidden email]> wrote:

> That would be because col is a function in base R, and thus a poor
> choice of names for user objects. Nonetheless, it worked when I ran
> it, but you didn't provide reproducible example so who knows.
>
> R> set.seed(1)
> R> x <- data.frame(matrix(runif(150), ncol=10))
> R> # col is a function, so not a good name
> R> col <- colMeans(x)
> R> mean(col)
> [1] 0.5119
>
> It's polite to include the list on your reply.
>
> Sarah
>
> On Wed, May 21, 2014 at 2:50 PM, Kate Ignatius <[hidden email]> wrote:
>> That didn't work: gave me the error =
>>
>> [1] NA
>> Warning message:
>> In mean.default(col) : argument is not numeric or logical: returning NA
>>
>> But writing it like: mean(colMeans(x, na.rm = TRUE, dims = 1)), worked
>>
>> Thanks!
>>
>> On Wed, May 21, 2014 at 2:31 PM, Sarah Goslee <[hidden email]> wrote:
>>> Is
>>>
>>> mean(col)
>>>
>>> not what you're looking for?
>>>
>>> Sarah
>>>
>>> On Wed, May 21, 2014 at 2:26 PM, Kate Ignatius <[hidden email]> wrote:
>>>> Hi All,
>>>>
>>>> I've successfully gotten out the colMeans for 60 columns using:
>>>>
>>>> col <- colMeans(x, na.rm = TRUE, dims = 1)
>>>>
>>>> My next question is: is there a way of getting a mean of all the
>>>> column means (ie a mean of a mean)?
>>>>
>>>> Thanks!
>>>>
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Mean of colMeans

Frede Aakmann Tøgersen-2
In reply to this post by Sarah Goslee
Hhhhmmmmmmhhhm, but is that not the same as

mean(as.matrix(x))

?????

Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
[hidden email]
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender.


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of Sarah Goslee
> Sent: 21. maj 2014 21:11
> To: Kate Ignatius; r-help
> Subject: Re: [R] Mean of colMeans
>
> That would be because col is a function in base R, and thus a poor
> choice of names for user objects. Nonetheless, it worked when I ran
> it, but you didn't provide reproducible example so who knows.
>
> R> set.seed(1)
> R> x <- data.frame(matrix(runif(150), ncol=10))
> R> # col is a function, so not a good name
> R> col <- colMeans(x)
> R> mean(col)
> [1] 0.5119
>
> It's polite to include the list on your reply.
>
> Sarah
>
> On Wed, May 21, 2014 at 2:50 PM, Kate Ignatius <[hidden email]>
> wrote:
> > That didn't work: gave me the error =
> >
> > [1] NA
> > Warning message:
> > In mean.default(col) : argument is not numeric or logical: returning NA
> >
> > But writing it like: mean(colMeans(x, na.rm = TRUE, dims = 1)), worked
> >
> > Thanks!
> >
> > On Wed, May 21, 2014 at 2:31 PM, Sarah Goslee
> <[hidden email]> wrote:
> >> Is
> >>
> >> mean(col)
> >>
> >> not what you're looking for?
> >>
> >> Sarah
> >>
> >> On Wed, May 21, 2014 at 2:26 PM, Kate Ignatius
> <[hidden email]> wrote:
> >>> Hi All,
> >>>
> >>> I've successfully gotten out the colMeans for 60 columns using:
> >>>
> >>> col <- colMeans(x, na.rm = TRUE, dims = 1)
> >>>
> >>> My next question is: is there a way of getting a mean of all the
> >>> column means (ie a mean of a mean)?
> >>>
> >>> Thanks!
> >>>
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Mean of colMeans

Boris Steipe
Not necessarily. Missing values may base the means on unequal numbers of observation:

x <- c(1,1,1,2,NA,2)
dim(x) <- c(3,2)
cM <- colMeans(x, na.rm = TRUE, dims = 1)
mean(cM)
mean(x, na.rm = TRUE)

B.

On 2014-05-21, at 3:38 PM, Frede Aakmann Tøgersen wrote:

> Hhhhmmmmmmhhhm, but is that not the same as
>
> mean(as.matrix(x))
>
> ?????
>
> Yours sincerely / Med venlig hilsen
>
>
> Frede Aakmann Tøgersen
> Specialist, M.Sc., Ph.D.
> Plant Performance & Modeling
>
> Technology & Service Solutions
> T +45 9730 5135
> M +45 2547 6050
> [hidden email]
> http://www.vestas.com
>
> Company reg. name: Vestas Wind Systems A/S
> This e-mail is subject to our e-mail disclaimer statement.
> Please refer to www.vestas.com/legal/notice
> If you have received this e-mail in error please contact the sender.
>
>
>> -----Original Message-----
>> From: [hidden email] [mailto:[hidden email]]
>> On Behalf Of Sarah Goslee
>> Sent: 21. maj 2014 21:11
>> To: Kate Ignatius; r-help
>> Subject: Re: [R] Mean of colMeans
>>
>> That would be because col is a function in base R, and thus a poor
>> choice of names for user objects. Nonetheless, it worked when I ran
>> it, but you didn't provide reproducible example so who knows.
>>
>> R> set.seed(1)
>> R> x <- data.frame(matrix(runif(150), ncol=10))
>> R> # col is a function, so not a good name
>> R> col <- colMeans(x)
>> R> mean(col)
>> [1] 0.5119
>>
>> It's polite to include the list on your reply.
>>
>> Sarah
>>
>> On Wed, May 21, 2014 at 2:50 PM, Kate Ignatius <[hidden email]>
>> wrote:
>>> That didn't work: gave me the error =
>>>
>>> [1] NA
>>> Warning message:
>>> In mean.default(col) : argument is not numeric or logical: returning NA
>>>
>>> But writing it like: mean(colMeans(x, na.rm = TRUE, dims = 1)), worked
>>>
>>> Thanks!
>>>
>>> On Wed, May 21, 2014 at 2:31 PM, Sarah Goslee
>> <[hidden email]> wrote:
>>>> Is
>>>>
>>>> mean(col)
>>>>
>>>> not what you're looking for?
>>>>
>>>> Sarah
>>>>
>>>> On Wed, May 21, 2014 at 2:26 PM, Kate Ignatius
>> <[hidden email]> wrote:
>>>>> Hi All,
>>>>>
>>>>> I've successfully gotten out the colMeans for 60 columns using:
>>>>>
>>>>> col <- colMeans(x, na.rm = TRUE, dims = 1)
>>>>>
>>>>> My next question is: is there a way of getting a mean of all the
>>>>> column means (ie a mean of a mean)?
>>>>>
>>>>> Thanks!
>>>>>
>>
>> --
>> Sarah Goslee
>> http://www.functionaldiversity.org
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Mean of colMeans

arun kirshna


 Also, the ?colMeans() route seems to be slightly faster (in cases with no missing values).
set.seed(398)
x <- matrix(rnorm(1e4*2e4), ncol=2e4)

 system.time(res1 <- mean(colMeans(x)))
 # user  system elapsed
 #0.227   0.000   0.227



 system.time(res2 <- mean(x))
# user  system elapsed
# 0.46    0.00    0.46

all.equal(res1,res2)
#[1] TRUE

A.K.



On Wednesday, May 21, 2014 4:53 PM, Boris Steipe <[hidden email]> wrote:
Not necessarily. Missing values may base the means on unequal numbers of observation:

x <- c(1,1,1,2,NA,2)
dim(x) <- c(3,2)
cM <- colMeans(x, na.rm = TRUE, dims = 1)
mean(cM)
mean(x, na.rm = TRUE)

B.

On 2014-05-21, at 3:38 PM, Frede Aakmann Tøgersen wrote:

> Hhhhmmmmmmhhhm, but is that not the same as
>
> mean(as.matrix(x))
>
> ?????
>
> Yours sincerely / Med venlig hilsen
>
>
> Frede Aakmann Tøgersen
> Specialist, M.Sc., Ph.D.
> Plant Performance & Modeling
>
> Technology & Service Solutions
> T +45 9730 5135
> M +45 2547 6050
> [hidden email]
> http://www.vestas.com
>
> Company reg. name: Vestas Wind Systems A/S
> This e-mail is subject to our e-mail disclaimer statement.
> Please refer to www.vestas.com/legal/notice
> If you have received this e-mail in error please contact the sender.
>
>
>> -----Original Message-----
>> From: [hidden email] [mailto:[hidden email]]
>> On Behalf Of Sarah Goslee
>> Sent: 21. maj 2014 21:11
>> To: Kate Ignatius; r-help
>> Subject: Re: [R] Mean of colMeans
>>
>> That would be because col is a function in base R, and thus a poor
>> choice of names for user objects. Nonetheless, it worked when I ran
>> it, but you didn't provide reproducible example so who knows.
>>
>> R> set.seed(1)
>> R> x <- data.frame(matrix(runif(150), ncol=10))
>> R> # col is a function, so not a good name
>> R> col <- colMeans(x)
>> R> mean(col)
>> [1] 0.5119
>>
>> It's polite to include the list on your reply.
>>
>> Sarah
>>
>> On Wed, May 21, 2014 at 2:50 PM, Kate Ignatius <[hidden email]>
>> wrote:
>>> That didn't work: gave me the error =
>>>
>>> [1] NA
>>> Warning message:
>>> In mean.default(col) : argument is not numeric or logical: returning NA
>>>
>>> But writing it like: mean(colMeans(x, na.rm = TRUE, dims = 1)), worked
>>>
>>> Thanks!
>>>
>>> On Wed, May 21, 2014 at 2:31 PM, Sarah Goslee
>> <[hidden email]> wrote:
>>>> Is
>>>>
>>>> mean(col)
>>>>
>>>> not what you're looking for?
>>>>
>>>> Sarah
>>>>
>>>> On Wed, May 21, 2014 at 2:26 PM, Kate Ignatius
>> <[hidden email]> wrote:
>>>>> Hi All,
>>>>>
>>>>> I've successfully gotten out the colMeans for 60 columns using:
>>>>>
>>>>> col <- colMeans(x, na.rm = TRUE, dims = 1)
>>>>>
>>>>> My next question is: is there a way of getting a mean of all the
>>>>> column means (ie a mean of a mean)?
>>>>>
>>>>> Thanks!
>>>>>
>>
>> --
>> Sarah Goslee
>> http://www.functionaldiversity.org
>>
>> ______________________________________________
>> [hidden email] 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.

>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.