ok, here we go
#starting date
st.date<-as.Date("01-08-2005","%d-%m-%Y")
#your dates converted into Date data type
dates<-as.Date(date,"%d-%m-%Y")
#diff from st.date in days
(diff.d<-dates-st.date)
#diff in weeks
(diff.w<-(as.numeric(dates-st.date)+1)%/%7)
#or use explicitly built in function difftime, actually "-" uses it
internally, see ?difftime
(diff.wa<-floor(as.numeric(difftime(dates,st.date,units="weeks"))))
table(diff.w,hcrime)
Marco Helbich schrieb:
Hello Eik,
sorry that I contact you again, but I have still a problem with my
dates. I need a numeric starting point begining at 01-08-2005
(DDMMYYYY) for every case as a seperate column. E.g:
date day
01-08-2005 1
01-08-2005 1
02-08-2005 2
20-08-2005 20
Thank you for help and bear with me - I am a newbie!
Best regards
Marco
----- Original Message ----- From: "Eik Vettorazzi"
<e.vettora...@uke.uni-hamburg.de>
To: "Marco Helbich" <marco.helb...@gmx.at>
Cc: <r-help@r-project.org>
Sent: Tuesday, April 07, 2009 4:24 PM
Subject: Re: [R] calculate weeks for 2005 and aggregate them
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@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
______________________________________________
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.