Re: [R] calculating mean and s.d. from a two-column table

2010-09-30 Thread Peter Ehlers

On 2010-09-27 15:20, Joshua Wiley wrote:

Hi,

Peter's suggestion is more general, but for just the weighted mean,
there is a built in function you can use (I do not know of any basic
weighted standard deviation or variance functions).

dat<- data.frame(age = 1:5, no = c(21, 31, 9, 12, 6))
weighted.mean(x = dat$age, w = dat$no)

Best regards,

Josh




Hmisc has wtd.mean() and wtd.var() as well as a few other
weighted stats.

  -Peter Ehlers



On Mon, Sep 27, 2010 at 9:34 AM, Jonas Josefsson
  wrote:

I have a two-column table as follows where age is in the 1st column and the
number of individuals is in the 2nd.

age;no
1;21
2;31
3;9
4;12
5;6


Can I use mean() and sd() to calculate the mean and standard deviation from
this or do I have to manually multiplicate 21*1+31*2 etc. / N?

__
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
and provide commented, minimal, self-contained, reproducible code.







__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] calculating mean and s.d. from a two-column table

2010-09-28 Thread Jim Lemon

On 09/28/2010 02:34 AM, Jonas Josefsson wrote:

I have a two-column table as follows where age is in the 1st column and
the number of individuals is in the 2nd.

age;no
1;21
2;31
3;9
4;12
5;6


Can I use mean() and sd() to calculate the mean and standard deviation
from this or do I have to manually multiplicate 21*1+31*2 etc. / N?


Hi Jonas,
You can also use weighted.mean:

weighted.mean(age,no)

Jim

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] calculating mean and s.d. from a two-column table

2010-09-27 Thread Joshua Wiley
Hi,

Peter's suggestion is more general, but for just the weighted mean,
there is a built in function you can use (I do not know of any basic
weighted standard deviation or variance functions).

dat <- data.frame(age = 1:5, no = c(21, 31, 9, 12, 6))
weighted.mean(x = dat$age, w = dat$no)

Best regards,

Josh

On Mon, Sep 27, 2010 at 9:34 AM, Jonas Josefsson
 wrote:
> I have a two-column table as follows where age is in the 1st column and the
> number of individuals is in the 2nd.
>
> age;no
> 1;21
> 2;31
> 3;9
> 4;12
> 5;6
>
>
> Can I use mean() and sd() to calculate the mean and standard deviation from
> this or do I have to manually multiplicate 21*1+31*2 etc. / N?
>
> __
> 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
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] calculating mean and s.d. from a two-column table

2010-09-27 Thread Peter Langfelder
On Mon, Sep 27, 2010 at 9:34 AM, Jonas Josefsson
 wrote:
> I have a two-column table as follows where age is in the 1st column and the
> number of individuals is in the 2nd.
>
> age;no
> 1;21
> 2;31
> 3;9
> 4;12
> 5;6

You can use the following trick:

x = rep(age, no)

This repeats age[1] no[1]-times, age[2] no[2]-times, etc...

then use the usual mean and sd on x.

Peter

__
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
and provide commented, minimal, self-contained, reproducible code.


[R] calculating mean and s.d. from a two-column table

2010-09-27 Thread Jonas Josefsson
I have a two-column table as follows where age is in the 1st column and 
the number of individuals is in the 2nd.


age;no
1;21
2;31
3;9
4;12
5;6


Can I use mean() and sd() to calculate the mean and standard deviation 
from this or do I have to manually multiplicate 21*1+31*2 etc. / N?


__
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
and provide commented, minimal, self-contained, reproducible code.