简体   繁体   English

如何使用R和我自己的数据创建相似性矩阵?

[英]How do I create a similarity matrix using R with my own data?

I have a study where I presented pairs of stimuli and the individual recorded a response (numbers 1-1-1000). 我有一项研究,我提出了一对刺激,个人记录了一个反应(数字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. 我想要x轴上的刺激名称,y轴上的刺激2的名称以及根据所呈现的相应对记录的响应。 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. 现在我只在列中有我的数据:第1列试验编号,第2列刺激1的名称,第3列刺激2的名称,第4列是响应。 Any advice? 有什么建议? How could I go about this using R? 我怎么能用R来解决这个问题?

darckeen's post just needs to have list() instead of c() . darckeen的帖子只需要list()而不是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.) (顺便说一句,对不起,给我一个新的答案。我还没有足够的声誉来评论,编辑太小,无法直接申请上一篇文章。)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM