I thought it would be simple, but I got stuck and I would appreciate your help.
In my data there are 4 questions with Yes/No answers and they occur within weekly periods. The week periods have been numbered in dtf$Week
. What I need to do is to create a plot with weekly count of "Yes" answers on y axis and week number on x axis. The Yes scores should be represented with 4 lines in differing colours. Formatting is easy, but understanding how to properly summarize data unfortunately is not. I am only learning R.
> str(dtf)
'data.frame': 55 obs. of 7 variables:
$ id : num 7 8 9 10 11 12 13 16 17 18 ...
$ q_0001 : Factor w/ 2 levels "Yes","No": 1 1 1 1 1 1 2 1 1 1 ...
$ q_0002 : Factor w/ 2 levels "Yes","No": 2 1 1 1 2 2 2 2 2 2 ...
$ q_0003 : Factor w/ 2 levels "Yes","No": 2 2 2 1 2 2 2 2 2 2 ...
$ q_0004 : Factor w/ 2 levels "Yes","No": 1 1 1 1 1 1 2 2 2 2 ...
$ Assm_Date: Date, format: "2014-01-04" "2014-01-08" ...
$ Week : num 1 1 1 1 1 1 1 2 2 2 ...
The dataset can be reproduced with:
# sample dataset
Start <- as.Date("2014-01-03")
Dates<-vector()
class(Dates) <- "Date"
i=1
for (i in 1:8) {
End <- as.Date(Start+6)
Samp <- Start + sort(sample.int(End-Start, 7,replace=TRUE))
Dates <- append (Dates, Samp)
Start <- End + 1
i = i +1
}
id<-sort(sample(1:56))
q_0001<-sample(c("Yes","No"),56,replace=TRUE)
q_0002<-sample(c("Yes","No"),56,replace=TRUE)
q_0003<-sample(c("Yes","No"),56,replace=TRUE)
q_0004<-sample(c("Yes","No"),56,replace=TRUE)
dtf<-data.frame(id,Dates,q_0001,q_0002,q_0003,q_0004)
dtf$Week = 1 + as.numeric(dtf$Date - as.Date("2014-01-03")) %/% 7
rm(i,Dates,Start,End,Samp,id,q_0001,q_0002,q_0003,q_0004)
If need be a link to the test dataset also is here. Sorry I did not make the test dataset right away, I am a bit new to this.