0

I have problem with adding a matrix I created using tappy fuction. The following is the code I run:

colnames(learningblock)
learningblock$uniquecode <- paste(learningblock$subject,learningblock$condition,learningblock$index,sep="_")
colnames(learningblock)
head(learningblock)
FF2 = learningblock[learningblock$CURRENT_FIX_INDEX == 1 | learningblock$CURRENT_FIX_INDEX == 2,]
FF2.1=FF2[FF2$CURRENT_FIX_INTEREST_AREA_FIX_COUNT>1,]
head(FF2.1)
FF2.2=FF2.1
sum.matrix = tapply(FF2.2$CURRENT_FIX_DURATION, FF2.2$uniquecode, sum, na.rm = T)
FF2.2$sum.matrix <- rep(sum.matrix, each=2)
head(FF2.2)

What I aimed to do is the sum the numbers based on "uniquecode", for example, from the oupput image I added, I want to do 236+643=879; 190+387=577...., but when I checked the column of sum.matrix the numbers look not correct it should not be 698, 512... I guess the sum.matrix doesn't match the unicode when I added into the dataframe. Can you help you to sort this out?

Thank you very much!

enter image description here

etienne
  • 3,648
  • 4
  • 23
  • 37
Mengsi
  • 1
  • Welcome to StackOverflow. Please take a look at these tips on how to produce a [minimum, complete, and verifiable example](http://stackoverflow.com/help/mcve), as well as this post on [creating a great example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). From your description, I think you want to use `ave`: `FF2.2$sumValues <- ave(FF2.2$CURRENT_FIX_DURATION, FF2.2$uniquecode, sum, na.rm = T)`. – lmo Nov 23 '16 at 16:21
  • Thank you for your answer. But what I am trying to do is sum the two numbers in case that they have same uniquecode. tapply fuction works well. But when I add the sum.matrix as in new column, the problem is that this colume was not displayed based on "uniquecode". In other words, the right values within column for "sum.matrix" should be 879,879,577,577... Thank you very much for the tips on how to creat a good question.:-) – Mengsi Nov 23 '16 at 16:52
  • Thank you very much. I sorted it using the code you provided. – Mengsi Nov 23 '16 at 17:56

0 Answers0