[R] calculate weeks for 2005 and aggregate them
Eik Vettorazzi
E.Vettorazzi at uke.uni-hamburg.de
Tue Apr 7 16:24:26 CEST 2009
Hi Marco,
try
date <- c("20-08-2005", "27-08-2005", "19-08-2005", "29-08-2005",
"01-08-2005", "01-08-2005",
"25-08-2005", "15-08-2005", "11-08-2005", "12-08-2005", "18-08-2005",
"02-08-2005",
"04-08-2005", "15-08-2005", "17-08-2005", "24-08-2005", "15-08-2005",
"30-08-2005",
"30-08-2005", "02-08-2005", "05-08-2005", "04-08-2005", "22-08-2005",
"11-08-2005",
"25-08-2005", "29-08-2005", "07-08-2005", "04-08-2005", "08-08-2005",
"08-08-2005")
# your given crime-"factor" had only 3 entries
hcrime <- factor(sample(1:3,length(date),replace=T),labels=c("Aggravated
Assault", "Burglary", "Robbery"))
#depending on your starting day of a week you could also use "%U%
instead of "%W", see ?strftime
week<-as.factor(format(as.Date(date,"%d-%m-%Y"),"%W"))
#step 2 and 3
table(week,hcrime)
but you should explain why 27-08-2005 will lead to week=99?!
hth.
Marco Helbich schrieb:
> Dear list,
>
> here is an example of my dataset:
>
> date <- c("20-08-2005", "27-08-2005", "19-08-2005", "29-08-2005",
> "01-08-2005", "01-08-2005",
> "25-08-2005", "15-08-2005", "11-08-2005", "12-08-2005", "18-08-2005",
> "02-08-2005",
> "04-08-2005", "15-08-2005", "17-08-2005", "24-08-2005", "15-08-2005",
> "30-08-2005",
> "30-08-2005", "02-08-2005", "05-08-2005", "04-08-2005", "22-08-2005",
> "11-08-2005",
> "25-08-2005", "29-08-2005", "07-08-2005", "04-08-2005", "08-08-2005",
> "08-08-2005")
> hcrime <- factor(c("Aggravated Assault", "Burglary", "Robbery"))
> data <- cbind(date, hcrime)
>
> I have following questions:
> a) how can I find out the weeks of every date
> b) aggregate the dates falling in the same week
> c) sum up the different crimes per week
>
> for better understanding here is an example for a) and b):
> date week
> 27-08-2005 99
> 27-08-2005 99
> 29-08-2005 99
>
> and here one for c):
>
> week crimes_Robbery crimes_Robbery
> 98 2 1
> 99 3 0
>
> thank you very much for your help!
> best regards
> Marco
>
> ______________________________________________
> R-help at 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.
--
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf
Martinistr. 52
20246 Hamburg
T ++49/40/42803-8243
F ++49/40/42803-7790
More information about the R-help
mailing list