Re: [R] apply a function over a factor

2009-12-29 Thread Dimitris Rizopoulos

have a look at ?ave(), e.g.,

A - 1:10
B - factor(c(1,2,2,4,4,4,7,7,7,7))

ave(A, B)


I hope it helps.

Best,
Dimitris


farida...@gmail.com wrote:

Dear R experts,

I would like to substitute the values of a vector, say A, with the average  
values taken over a factor


For example, lets assume


A

[1] 1 2 3 4 5 6 7 8 9 10

B

[1] 1 2 2 4 4 4 7 7 7 7
Levels: 1 2 4 7

I need to have

1.0 2.5 2.5 5.0 5.0 5.0 8.5 8.5 8.5 8.5 8.5

Thank you

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



--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014

__
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] apply a function over a factor

2009-12-29 Thread Farida Mostajabi
Thanks! That's what I need.

On Tue, Dec 29, 2009 at 1:33 PM, Dimitris Rizopoulos 
d.rizopou...@erasmusmc.nl wrote:

 have a look at ?ave(), e.g.,

 A - 1:10
 B - factor(c(1,2,2,4,4,4,7,7,7,7))

 ave(A, B)


 I hope it helps.

 Best,
 Dimitris


 farida...@gmail.com wrote:

 Dear R experts,

 I would like to substitute the values of a vector, say A, with the average
  values taken over a factor

 For example, lets assume


 A

 [1] 1 2 3 4 5 6 7 8 9 10

 B

 [1] 1 2 2 4 4 4 7 7 7 7
 Levels: 1 2 4 7

 I need to have

 1.0 2.5 2.5 5.0 5.0 5.0 8.5 8.5 8.5 8.5 8.5

 Thank you

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


 --
 Dimitris Rizopoulos
 Assistant Professor
 Department of Biostatistics
 Erasmus University Medical Center

 Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
 Tel: +31/(0)10/7043478
 Fax: +31/(0)10/7043014


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