1

that is my first question on Stack. I am looking for an efficient way to group and sum values inside a row. My data in row is distributed that way: 10000 10 20000 20 40000 12 60000 23 10000 12 40000 17. I need a result: 10000 22 20000 20 40000 29 60000 23. Big numbers are characteristics, smaller - occurences.

example of row from data

Thats my real data - for example for 10004 I need 23. I tried with loops, aggregate, but I cannot do this.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Adamek
  • 95
  • 8
  • 4
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Nov 19 '17 at 16:48

1 Answers1

0

You can do this with aggregate

## Your data
Row = c(11131132, 10004, 5, 265098, 3, 190029, 2, 
    265092, 2, 265077, 17, 10004, 18, 
    265097,17, 265098,15,190029,2, 265099,17)

ID    = Row[seq(2,length(Row), 2)]
value = Row[seq(3,length(Row), 2)]
GroupSum = aggregate(value, list(ID), sum)
GroupSum
  Group.1  x
1   10004 23
2  190029  4
3  265077 17
4  265092  2
5  265097 17
6  265098 18
7  265099 17
G5W
  • 36,531
  • 10
  • 47
  • 80