I have created a survey form and I collect the results in R. There are many questions to the survey and they are grouped into sections. For instance the first three questions are under the category: Behaviors. The user is able to go back into the survey after their initial submission to update their answers (all questions are True/False). I am struggling to develop a cumulative score over time given that users can go back in and update their answers. Below is an example of the survey data results for the Behaviors category:
SurveyData:
I would like to develop a score that will take the most recent survey result for a user, but I need to keep the previous submission data to show progress. For example, this is the result I would like:
Result:
The formula for the Score column would be (#Trues for that day/(number_of_submissions for that day/number of questions for that category). The number of questions for the Behaviors category is 3. However, I am struggling how to calculate the score when users re-submit the survey with new answers. Any idea how I can do this in R?
This is my current approach, which calculates the cumulative score, but does not take into account if a user submitted a survey more than once, as it does not override and take the most recent survey result.
Reproducible Example:
user <- c('User1', 'User1', 'User1', 'User2', 'User3', 'User3')
submission_date <- as.Date(c('2016-09-04', '2016-09-15', '2016-09-29', '2016-09-04', '2016-09-05', '2016-09-20'))
Trues_Behavior_Category <- c(1,2,3,3,2,3)
survey_data <- data.frame(user, submission_date, Trues_Behavior_Category)
checklist_data_weekly <- aggregate(survey_data[c(3)], list(Sub_Date = as.Date(survey_data$submission_date)), sum)
checklist_data_cum <- cbind(checklist_data_weekly[c(1)], cumsum(checklist_data_weekly[c(2)]))
sub_count <- aggregate(survey_data[c(1)], list(Sub_Date = as.Date(survey_data$submission_date)), length)
sub_count <- cbind(sub_count[c(1)], Cum_Dates=cumsum(sub_count$user))
checklist_data_cum$Behaviors_Score <- (checklist_data_cum$Trues_Behavior_Category/(sub_count$Cum_Dates*3)*100)