I have a data frame like the one below:
dat<-data.frame (col1= c(rep("A", 10), rep("B", 10)),
test= c(rep("pre", 5), rep("post", 5), rep("pre", 5), rep("post", 5)),
ID= c("ID_A","ID_B","ID_C","ID_D","ID_E","ID_A","ID_B","ID_C", "ID_D","ID_E","ID_F","ID_G","ID_H","ID_I","ID_J","ID_F","ID_G","ID_H","ID_I","ID_J"),
answer= c("1_2_3", "4_6_8", "9_4_1", "3_7_2", "5_7_4", "1_2_3", "6_8_4", "9_4_2", "3_9_1", "5_4_7", "6_5_4", "4_6_8", "3_7_1", "6_7_9", "5_7_4", "8_9_1", "6_8_4", "7_1_4", "6_8_3", "5_4_7"))
Each level of ID has two levels in test - which represent responses to a pre and post test. answer represents the respondents answer.
I wish to summarize the following result data frame res:
res<-data.frame(ID= c("ID_A","ID_B","ID_C", "ID_D","ID_E", "ID_F","ID_G","ID_H","ID_I","ID_J"),
res1=c("yes", "yes","no", "no", "yes", "no", "yes","no", "no", "yes"), #match? yes or no
res2=c(0,0,1, 2, 0, 3, 0,1, 2, 0 ),#count of the new answer components
res3= c(NA, NA, 2, "9_1", NA,"8_9_1",NA, 4, "8_3", NA)) #string specifying new answer components
res summarizes matches in answer for each level of ID irrespective of order and returns:
col1a binary variable that summarizes the presence or absence of a match between the pre and post testcol2a count of the number of new entries in the post test. Values can only be between 0-3 since there is a limit of 3 values for each answer.col3a string that specifies the new answer components present in the post test but not in the pre test.