-4

I want to draw this plot:

from these data. The beginning of the data:

x <- read.csv2(text=
"strategy;success;date
1;1;02.09.2007
1;0;4/19/2012
1;1;2/15/2013
1;0;3/17/2004
1;1;12.02.2004
2;1;7/28/2014
1;1;9/29/2003
1;1;04.02.2004
1;1;09.02.2004
1;1;12/21/2004
1;1;6/23/2005
1;1;7/26/2005
1;1;8/24/2005
1;1;12.08.2005
1;1;01.10.2006
1;1;1/27/2006
1;1;07.10.2006
1;1;8/31/2006
1;1;3/20/2007
1;1;4/26/2007
1;1;6/27/2007
1;1;6/26/2007
1;1;7/16/2007
1;1;08.07.2007
1;1;2/26/2008
1;1;03.05.2008
1;1;04.10.2008
1;1;
1;1;09.01.2008
1;1;10/15/2008
1;1;10/23/2008
1;1;12/31/2008
1;1;1/22/2009")

I want to select only data which have an 1 in column 1:

x_selected <- x[x[,1]=="1", ]

I realised that the column with the dates is the wrong class and very heterogenous:

class(x_selected[,3])
## "factor"

Then i wanted to change this ugly dates (not heterogenous) into dates, but I cant figure out how to adapt the dates? My best try failed:

for (i in 1:nrow(x_selected)){
    x_selected[i,3] <- as.Date(x_selected[i,3], "%d/%m/%Y")
}

My next step would be to group the single dates into quarters:

for (i in 1:nrow(x_selected)) {
   x_selected[i,3] <- round_date(x_selected[i, 3], unit = c("quarter"))
}

I haven't been able to get any further. :-(

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
fank
  • 9
  • 1
  • 4
  • Nobody is particularly inclined to go download data from a sketchy link. Instead edit a relevant subset of your data into your question, ideally made with `dput`. [Read this for more details.](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – alistaire Sep 02 '16 at 22:45
  • To parse the dates, `lubridate::mdy(x$date)`. For quarters, wrap that in `lubridate::quarter(..., with_year = TRUE)` or more usefully `zoo::as.yearqtr`. – alistaire Sep 03 '16 at 01:32

1 Answers1

1

Here's a start:

 x2 <- subset(x,strategy==1)

Replace . separators with /

 x2 <- transform(x2,date=gsub("\\.","/",as.character(date)))

Convert to date

 x2$date2 <- as.Date(x2$date,format="%m/%d/%Y")

Convert to year/quarter

 x2$q <- zoo::as.yearqtr(x2$date2)

Proportion of successes by quarter:

 with(x2,tapply(success,list(q),mean))
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 1
    Could go golfing and do `chartr(".","/",date)` in place of `gsub`. But I think `gsub` will do `as.character` internally too. – Rich Scriven Sep 04 '16 at 02:02