0

I am trying to calculate the average number participants scored correct on a memory task. I have a column called RecallType which tells me if participants were assessed through forward memory recall (called forwards) or through backwards memory recall (called backwards). I also have a column called ProbeState which identifies the type of memory task, of which there are two. In this column I have positions and digits. These are all my variables of interest.

The memory task itself is split by two columns. Recall.CRESP is a column specifying the correct answers on a memory test selected through grid coordinates. Recall.RESP shows participants response.

These columns look something like this:

|Recall.CRESP                     | Recall.RESP                     |
|---------------------------------|---------------------------------|                 
|grid35grid51grid12grid43grid54   | grid35grid51grid12grid43grid54  |                
|grid11gird42gird22grid51grid32   | grid11gird15gird55grid42grid32  |

So for example in row 1 of this table, the participant got 5/5 correct as the grid coordinates of Recall.CRESP matches with Recall.RESP. However in row 2, the participant only got 2/5 correct as only the first and the last grid coordinate are identical. The order of the coordinates must match to be correct.

Ideally I would love to learn from any response. If you do reply please kindly put some comments.

Thanks.

Rstudent
  • 75
  • 9
  • Is your question how to count the number of correct responses? The answer should be 5 for row 1 and 2 for row 2? – Divi Jan 08 '18 at 18:18
  • I suppose that would be a better way to word it! Count the correct responses yes, and give the mean by variable of interest. Forwards digit recall is on average eg. 4 out of 5 correct across rows. – Rstudent Jan 08 '18 at 18:28

2 Answers2

1

As you are new to stackoverflow, please read the answer here on how to make a reproducible example so your question is clear: How to make a great R reproducible example?.

From what I understand, you are looking to split your string and then count the equal cases. Some code to get you started on that is below:

a = "grid11gird42gird22grid51grid32"
b = "grid11gird15gird55grid42grid32"

a1 = strsplit(a, "grid|gird")
b1 = strsplit(b, "grid|gird")
table(unlist(a1) == unlist(b1))["TRUE"] - 1

You should be able to take mean by your variable of interest using group_by and summarize functionality of package dplyr.

Divi
  • 1,614
  • 13
  • 23
1

Try using regmatches

fun=function(x)do.call(rbind,regmatches(x,gregexpr(".*?\\d.",x)))
with(dat,rowSums(fun(Recall.CRESP)==fun(Recall.RESP)))
[1] 5 2

DATA:

structure(list(Recall.CRESP = c("grid35grid51grid12grid43grid54", 
"grid11grid42grid22grid51grid32"), Recall.RESP = c("grid35grid51grid12grid43grid54", 
"grid11grid15grid55grid42grid32")), .Names = c("Recall.CRESP", 
"Recall.RESP"), row.names = c(NA, -2L), class = "data.frame")
Onyambu
  • 67,392
  • 3
  • 24
  • 53