0

N* [1]| [2] | [3]
1* | 3 | 20 | 3 |
2* | 2 | 10 | 3 |
3* | 3 | 25 | 3 |
4* | 1 | 15 | 3 |
5* | 3 | 30 | 3 |

Can you help me to get a sum of second column, but only sum of elements that has 3 in the first row. For example in that matrix it is 20+25+30=75. In a fastest way (it's actually big matrix).

P.S. I tried something like this with(Train, sum(Column2[,"Date"] == i)) As you can see I need sum Of Colomn2 where date has certain meaning (from 1 to 12)

1 Answers1

2

We can create a logical index with the first column and use that to subset the second column and get the sum

 sum(m1[m1[,1]==3,2])

EDIT: Based on @Richard Scriven's comment.

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you, can you recomend some R tutorial with a lot of information? – Максим Усик Oct 01 '15 at 17:24
  • @МаксимУсик [edx](https://www.edx.org/course/introduction-r-programming-microsoft-dat204x-0?gclid=CJvijbHlocgCFRcXjgod8TQH9g) started a course recently. Also check `coursera` [datacamp](https://www.datacamp.com/) and the links [here](http://stackoverflow.com/questions/3375808/learning-r-where-does-one-start) – akrun Oct 01 '15 at 17:28