Combining data using R (or maybe Excel) -- looping to match stimuli

62 views Asked by At

I have two sets of data, which correspond to different experiment tasks that I want to merge for analysis. The problem is that I need to search and match up certain rows for particular stimuli and for particular participants. I'd like to use a script to save some trouble. This is probably quite simple, but I've never done it before.

Here's my problem more specifically: In the first data set, each row corresponds to a two-alternative forced choice task where two stimuli are presented at a time and the participant selects one. In the second data set, each row corresponds to a single item task where the participants are asked if they have ever seen the stimulus before. The stimuli in the second task match the stimuli in the pairs on the first task (twice as many rows). I want to be able to match up and add two columns to the first dataset--one that states if the leftside item was recognized later and one for the rightside stimulus.

I assume this could be done with nested loops, but I'm not sure if there is a elegant way to do this or perhaps a package.

1

There are 1 answers

1
josliber On

As I understand it, your first dataset looks something like this:

(dat1 <- data.frame(person=1:2, stim1=1:2, stim2=3:4))
#   person stim1 stim2
# 1      1     1     3
# 2      2     2     4

This would mean person 1 got stimuli 1 and 3 and person 2 got stimuli 2 and 4. Then your second dataset looks something like this:

(dat2 <- data.frame(person=c(1, 1, 2, 2), stim=c(1, 3, 4, 2), responded=c(0, 1, 0, 1)))
#   person stim responded
# 1      1    1         0
# 2      1    3         1
# 3      2    4         0
# 4      2    2         1

This gives information about how each person responded to each stimulus they were given.

You can merge these two by matching person/stimulus pairs with the match function:

dat1$response1 <- dat2$responded[match(paste(dat1$person, dat1$stim1), paste(dat2$person, dat2$stim))]
dat1$response2 <- dat2$responded[match(paste(dat1$person, dat1$stim2), paste(dat2$person, dat2$stim))]
dat1
#   person stim1 stim2 response1 response2
# 1      1     1     3         0         1
# 2      2     2     4         1         0

Another option (starting from the original dat1 and dat2) would be to merge twice with the merge function. You have a little less control on the names of the output columns, but it requires a bit less typing:

merged <- merge(dat1, dat2, by.x=c("person", "stim1"), by.y=c("person", "stim"))
merged <- merge(merged, dat2, by.x=c("person", "stim2"), by.y=c("person", "stim"))