Dear list users,
I have a data frame as below specified.
From the 1st of May to the 30th of September of several years (e.g. from 2004 to
2011) I have a frequency of accidents.
I need the mean of accidents divided by weeks (i.e. the mean of accidents from
the 1st to the 7th of May of all the years, from the 8th to the 14th of May,
..., from the 29th to the 31st of May, from the 1st to the 7th of July and so
on).
Is there an easy way to do that?
Thank you for your help
Stefano Sofia
year month day Freq
1 2004 5 1 3
2 2004 5 10 2
3 2004 5 11 2
4 2004 5 12 2
5 2004 5 13 3
6 2004 5 14 0
7 2004 5 15 2
8 2004 5 16 1
9 2004 5 17 6
10 2004 5 18 1
11 2004 5 19 2
12 2004 5 2 4
13 2004 5 20 0
14 2004 5 21 0
15 2004 5 22 3
16 2004 5 23 4
17 2004 5 24 3
18 2004 5 25 2
19 2004 5 26 2
20 2004 5 27 0
21 2004 5 28 2
22 2004 5 29 3
23 2004 5 3 2
24 2004 5 30 3
25 2004 5 31 7
26 2004 5 4 1
27 2004 5 5 2
28 2004 5 6 3
29 2004 5 7 3
30 2004 5 8 1
31 2004 5 9 1
32 2004 6 1 3
33 2004 6 10 1
34 2004 6 11 3
35 2004 6 12 1
36 2004 6 13 3
37 2004 6 14 1
38 2004 6 15 1
AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu? contenere
informazioni confidenziali, pertanto ? destinato solo a persone autorizzate alla
ricezione. I messaggi di posta elettronica per i client di Regione Marche
possono contenere informazioni confidenziali e con privilegi legali. Se non si ?
il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo
messaggio. Se si ? ricevuto questo messaggio per errore, inoltrarlo al mittente
ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell?art.
6 della DGR n. 1394/2008 si segnala che, in caso di necessit? ed urgenza, la
risposta al presente messaggio di posta elettronica pu? essere visionata da
persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by persons
entitled to receive the confidential information it may contain. E-mail messages
to clients of Regione Marche may contain information that is confidential and
legally privileged. Please do not read, copy, forward, or store this message
unless you are an intended recipient of it. If you have received this message in
error, please forward it to the sender and delete it completely from your
computer system.
Stefano, On 13 April 2012 20:51, Stefano Sofia <stefano.sofia@regione.marche.it>wrote:> I have a data frame as below specified. > From the 1st of May to the 30th of September of several years (e.g. from > 2004 to 2011) I have a frequency of accidents. > I need the mean of accidents divided by weeks (i.e. the mean of accidents > from the 1st to the 7th of May of all the years, from the 8th to the 14th > of May, > ..., from the 29th to the 31st of May, from the 1st to the 7th of July and > so on). > Is there an easy way to do that? >Take a look at https://bitbucket.org/hd1/financeocr/raw/13c4990bd21b/visualizations/dayAmountsHist.Rto get the subsets and sapply(list, mean, simplify=TRUE). -- H -- Sent from my mobile device Envoyait de mon portable [[alternative HTML version deleted]]
Dear Stefano,
A practical way might be as the following
R> acc<-read.table("acc.txt",header=T) #reading your data into R
R> acc.may<-acc[acc[,3]==5,] #subsetting data with respect to may
R> acc.may.order<-acc.may[order(acc.may[,4]),] #ordering with respect to
day
R> mean(acc.may.order[1:7,5]) # mean of the period of day 1 to 7
R> mean(acc.may.order[8:14,5]) # mean of the period of day 8 to 15
This script is just for May and taking means needs manual manipulations. On
the other hand, simple loops might deal with your problem such as for loop.
Best
Ozgur
-----
************************************
Ozgur ASAR
Research Assistant
Middle East Technical University
Department of Statistics
06531, Ankara Turkey
Ph: 90-312-2105309
http://www.stat.metu.edu.tr/people/assistants/ozgur/
--
View this message in context:
http://r.789695.n4.nabble.com/how-to-divide-data-by-week-tp4556650p4556740.html
Sent from the R help mailing list archive at Nabble.com.