1

I am looking at some data from a cognitive task in which each trial contains four items : a visual mask ("premask"), a visual prime ("prime"), a target to which a response is inputted ("target"), and a visual mask ("postmask"). For one participant, I am missing two targets somewhere. They have only 1790 of 1792 'items'(448 trials). Using sum(with(subjectnumber, item type == 'target'), I was able to determine it is two targets rather than primes or masks I am missing in this dataframe, as it returned two less than I expected. The problem now is finding where the deviation from the Premask, Prime, Target, Postmask pattern happens without manually scrolling through all 1790 items. The trials begin and end exactly how I expect them to, so this tells me this occurs somewhere in the middle.

What I need is for R to return the 'item' variable (numerical with range of 1-1790) when the pattern of the 'item type' variable (Premask, Prime, Target, Postmask) is broken. I am new to the programming/R world so I'm not sure where I should start. In the example below I would need it to return '11'. Apologizes in advance if anything is unclear. Any help is appreciated!

item Item Type
1 Premask
2 Prime
3 Target
4 Postmask
5 Premask
6 Prime
7 Target
8 Postmask
9 Premask
10 Prime
11 Postmask
12 Premask
Paul
  • 83
  • 6
  • 1
    Please make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Martin Gal Aug 04 '21 at 18:05
  • To be clear: "Item Type" follows the pattern "Premask", "Prime", "Target" and "Postmask" and you are trying to find the item, when this pattern is broken? – Martin Gal Aug 04 '21 at 18:22
  • Yes, sorry for the confusion! I added a quick example. – Paul Aug 04 '21 at 18:34
  • Finding the first one is quite simple: `df[df$Item_Type != "Target" & df$item %% 4 == 3,]`. I'm struggling with finding both... – Martin Gal Aug 04 '21 at 18:40

1 Answers1

-1

I would try this:

df[c(which.max(df$Item_Type == "Postmask" & df$item %% 4 == 3),
     which.max(df$Item_Type == "Postmask" & df$item %% 4 == 2)), ]

Since "Target" is missing, we look for the first "Postmask", whose item number is decreased by 1. Next we are looking for the first "Postmask" item, whose item number changes by 2.

Martin Gal
  • 16,640
  • 5
  • 21
  • 39