Re: [R] basic problem but can't solve it

2007-05-23 Thread Christophe Pallier
If I understood correctly, the initial post asked for a vector of the same
length as the original one. This is why I suggested:

   tapply(Measure,Month,mean)[as.character(Month)]

btw, this is handy way to compute deviations from the means of subgroups (x
- tapply(x, group, mean)[as.character(group)])

Christophe Pallier


On 5/22/07, John Kane [EMAIL PROTECTED] wrote:

 aggregate(Measure, list(Month=Month), mean)

 --- Benoit Chemineau [EMAIL PROTECTED]
 wrote:

  Hello,
 I have a basic problem but i can't figure it out
  with the
  table underneath. I would like to compute monthly
  averages.
 I would like to have the average measure for
  month #5 for the first
  three rows (the same number in the first three
  lines) and the average
  measure for month #6 for the last four rows ((the
  same number in the first
  three lines) in a separate vesctor (let's call it
  'result')
 I tried to use a while statement inside a for
  loop but it doesn't
  seem to work.
 Can someone please help me with this ?
 
 Measure Month
 2.28 5
 14.04 5
 0.60 5
 0.21 6
 0.96 6
 0.75 6
 1.28 6
 
 Thank you !
 
[[alternative HTML version deleted]]
 
  __
  R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.




-- 
Christophe Pallier (http://www.pallier.org)

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] basic problem but can't solve it

2007-05-23 Thread Gabor Grothendieck
Use ave.  Assuming DF is your 2 column data frame:

ave(DF[,1], DF[,2], FUN = mean)

On 5/22/07, Benoit Chemineau [EMAIL PROTECTED] wrote:
 Hello,
   I have a basic problem but i can't figure it out with the
 table underneath. I would like to compute monthly averages.
   I would like to have the average measure for month #5 for the first
 three rows (the same number in the first three lines) and the average
 measure for month #6 for the last four rows ((the same number in the first
 three lines) in a separate vesctor (let's call it 'result')
   I tried to use a while statement inside a for loop but it doesn't
 seem to work.
   Can someone please help me with this ?

   Measure Month
   2.28 5
   14.04 5
   0.60 5
   0.21 6
   0.96 6
   0.75 6
   1.28 6

   Thank you !

[[alternative HTML version deleted]]

 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] basic problem but can't solve it

2007-05-22 Thread Benoit Chemineau
Hello,
   I have a basic problem but i can't figure it out with the
table underneath. I would like to compute monthly averages.
   I would like to have the average measure for month #5 for the first
three rows (the same number in the first three lines) and the average
measure for month #6 for the last four rows ((the same number in the first
three lines) in a separate vesctor (let's call it 'result')
   I tried to use a while statement inside a for loop but it doesn't
seem to work.
   Can someone please help me with this ?

   Measure Month
   2.28 5
   14.04 5
   0.60 5
   0.21 6
   0.96 6
   0.75 6
   1.28 6

   Thank you !

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] basic problem but can't solve it

2007-05-22 Thread Cody_Hamilton

Benoit,

Try the aggregate() function.

-Cody

Cody Hamilton, PhD
Edwards Lifesciences



   
 Benoit   
 Chemineau
 benoitchemineau@  To 
 gmail.comr-help@stat.math.ethz.ch
 Sent by:   cc 
 [EMAIL PROTECTED] 
 at.math.ethz.ch   Subject 
   [R] basic problem but can't solve   
   it  
 05/22/2007 10:01  
 AM
   
   
   
   




Hello,
   I have a basic problem but i can't figure it out with the
table underneath. I would like to compute monthly averages.
   I would like to have the average measure for month #5 for the first
three rows (the same number in the first three lines) and the average
measure for month #6 for the last four rows ((the same number in the first
three lines) in a separate vesctor (let's call it 'result')
   I tried to use a while statement inside a for loop but it doesn't
seem to work.
   Can someone please help me with this ?

   Measure Month
   2.28 5
   14.04 5
   0.60 5
   0.21 6
   0.96 6
   0.75 6
   1.28 6

   Thank you !

 [[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] basic problem but can't solve it

2007-05-22 Thread Gavin Simpson
On Tue, 2007-05-22 at 19:01 +0200, Benoit Chemineau wrote:
 Hello,
I have a basic problem but i can't figure it out with the
 table underneath. I would like to compute monthly averages.
I would like to have the average measure for month #5 for the first
 three rows (the same number in the first three lines) and the average
 measure for month #6 for the last four rows ((the same number in the first
 three lines) in a separate vesctor (let's call it 'result')
I tried to use a while statement inside a for loop but it doesn't
 seem to work.
Can someone please help me with this ?
 
Measure Month
2.28 5
14.04 5
0.60 5
0.21 6
0.96 6
0.75 6
1.28 6

If dat is a data frame containing your data:

 dat
  Measure Month
12.28 5
2   14.04 5
30.60 5
40.21 6
50.96 6
60.75 6
71.28 6

 aggregate(dat$Measure, by = list(Month = dat$Month), mean)
  Monthx
1 5 5.64
2 6 0.80

 tapply(dat$Measure, dat$Month, mean)
   56
5.64 0.80

see ?aggregate and ?tapply for two solutions. The tapply one seems
cleaner and easier to get the vector you need, the aggregate version
needs an extra step:

aggregate(dat$Measure, by = list(Month = dat$Month), mean)$x
  ^^
Note the $x at the end to subset the object returned by aggregate

HTH

G

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Gavin Simpson [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,  [f] +44 (0)20 7679 0565
 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London  [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

__
R-help@stat.math.ethz.ch 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] basic problem but can't solve it

2007-05-22 Thread Christophe Pallier
tapply(Measure,Month,mean)[as.character(Month)]

-- 
Christophe Pallier (http://www.pallier.org)


On 5/22/07, Benoit Chemineau [EMAIL PROTECTED] wrote:

 Hello,
I have a basic problem but i can't figure it out with the
 table underneath. I would like to compute monthly averages.
I would like to have the average measure for month #5 for the first
 three rows (the same number in the first three lines) and the average
 measure for month #6 for the last four rows ((the same number in the first
 three lines) in a separate vesctor (let's call it 'result')
I tried to use a while statement inside a for loop but it doesn't
 seem to work.
Can someone please help me with this ?

Measure Month
2.28 5
14.04 5
0.60 5
0.21 6
0.96 6
0.75 6
1.28 6

Thank you !

 [[alternative HTML version deleted]]

 __
 R-help@stat.math.ethz.ch 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.


[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] basic problem but can't solve it

2007-05-22 Thread John Kane
aggregate(Measure, list(Month=Month), mean) 

--- Benoit Chemineau [EMAIL PROTECTED]
wrote:

 Hello,
I have a basic problem but i can't figure it out
 with the
 table underneath. I would like to compute monthly
 averages.
I would like to have the average measure for
 month #5 for the first
 three rows (the same number in the first three
 lines) and the average
 measure for month #6 for the last four rows ((the
 same number in the first
 three lines) in a separate vesctor (let's call it
 'result')
I tried to use a while statement inside a for
 loop but it doesn't
 seem to work.
Can someone please help me with this ?
 
Measure Month
2.28 5
14.04 5
0.60 5
0.21 6
0.96 6
0.75 6
1.28 6
 
Thank you !
 
   [[alternative HTML version deleted]]
 
 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.