3

I have a study where I presented pairs of stimuli and the individual recorded a response (numbers 1-1-1000). I would like the name of stimulus one on the x axis, the name of stimulus 2 on the y axis and the response recorded according to the corresponding pair presented. Right now I only have my data in columns: column 1 trial number, column 2 name of stim 1, column 3 name of stim 2, and column 4 the response. Any advice? How could I go about this using R?

joran
  • 169,992
  • 32
  • 429
  • 468
Jess
  • 31
  • 2
  • 2
    Welcome to Stack Overflow! It will be hard for anyone to help unless you provide a reproducible example that others can run on their own machines. This would include a small subset of your data (using dput()), for instance. See [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more info. – joran Sep 16 '11 at 20:16

2 Answers2

2

darckeen's post just needs to have list() instead of c(). Then I think it should work. Here's a full example:

set.seed(12345)
data = expand.grid(trial=1:10, stim1=1:5, stim2=1:3)
data = data.frame(data, response=rnorm(nrow(data)))

with(data, tapply(response, list(stim1,stim2), mean))

Output:

            1           2           3
1 -0.13294415  0.27326245 -0.11120045
2  0.28597776  0.02338804  0.21280916
3  0.08338741  0.44086561 -0.08682628
4  0.72432003  0.84250712  0.28383378
5 -0.06290978 -0.02588252 -0.36364019

(BTW sorry to make a new answer. I don't have enough reputation to comment yet and the edit is too small to apply directly to the previous post.)

John Colby
  • 22,169
  • 4
  • 57
  • 69
  • I'm not sure why this didn't get bumped up at the time but it should be now. Good piece of code here to keep around. – Rob Sep 24 '12 at 23:03
0

If understand the question correctly this should work:

mydata <- data.frame(trial,stim1,stim2,response)

mytable <- tapply(mydata$response,list(mydata$stim1,mydata$stim2),mean)
joran
  • 169,992
  • 32
  • 429
  • 468
darckeen
  • 960
  • 2
  • 12
  • 20