Let's say that I want to count the number of correct or incorrect responses in a row. If you look at the column "count," I pretty much want that, but I want it to start back at one every time there is a change from correct to incorrect and vice versa. I also want it to start back at one every time the condition or module changes.
I've found what should be two solutions to this problem...however, in my case they aren't working. Here is one of them: Running Count within groups in a dataframe. I believe that it isn't working because I also need to group by condition_id, which is numeric, and is just 1,2,3,4,5...all the way up until the sequence ends.
Thanks much!
dat%>%
group_by(pid, module, condition)%>%
arrange(pid, module, condition, condition_id)%>%
mutate(num.correct = ifelse(timing == "correct", 1, 0))%>%
group_by(pid, module, condition, num.correct)%>%
mutate(count = seq(n()))
dput:
structure(list(pid = c("ADMIN-UCSF-bo001", "ADMIN-UCSF-bo001",
"ADMIN-UCSF-bo001", "ADMIN-UCSF-bo001", "ADMIN-UCSF-bo001", "ADMIN-UCSF-bo001",
"ADMIN-UCSF-bo001", "ADMIN-UCSF-bo001", "ADMIN-UCSF-bo001", "ADMIN-UCSF-bo001",
"ADMIN-UCSF-bo001"), grade = c("3", "3", "3", "3", "3", "3",
"3", "3", "3", "3", "3"), gender = c("F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F"), Teacher = c("Keith, Susan", "Keith, Susan",
"Keith, Susan", "Keith, Susan", "Keith, Susan", "Keith, Susan",
"Keith, Susan", "Keith, Susan", "Keith, Susan", "Keith, Susan",
"Keith, Susan"), module = c("BOXED", "BOXED", "BOXED", "BOXED",
"BOXED", "BOXED", "BOXED", "BOXED", "BOXED", "BOXED", "BOXED"
), condition = c("Conjunction_4", "Conjunction_4", "Conjunction_4",
"Conjunction_4", "Conjunction_4", "Conjunction_4", "Conjunction_4",
"Conjunction_4", "Conjunction_4", "Conjunction_4", "Conjunction_4"
), trial_id = c(65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75),
condition_id = c(15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25), correct_button = c("correct", "correct", "correct",
"incorrect", "incorrect", "incorrect", "incorrect", "incorrect",
"correct", "incorrect", "correct"), rt = c(660.721957683563,
728.28596830368, 509.469985961914, 744.082987308502, 843.548953533173,
1161.27300262451, 961.09801530838, 928.547024726868, 711.355030536652,
710.889995098114, 877.265989780426), rw = c(1160, 1080, 920,
600, 640, 680, 760, 920, 1240, 1230, 1270), last = c(1270,
1270, 1270, 1270, 1270, 1270, 1270, 1270, 1270, 1270, 1270
), time = c("2017-04-07", "2017-04-07", "2017-04-07", "2017-04-07",
"2017-04-07", "2017-04-07", "2017-04-07", "2017-04-07", "2017-04-07",
"2017-04-07", "2017-04-07"), timing = c("correct", "correct",
"correct", "incorrect", "incorrect", "incorrect", "incorrect",
"incorrect", "correct", "incorrect", "correct")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -11L))