0

I have an 'Exp' data set which looks like this:

  Locals   Res   Ind
1    112 7.865 4.248
2    113 4.248 5.666
3    114 5.666 2.444
4    115 2.444 7.865
5    116 7.865 4.248
6    117 4.248 6.983
7    118 5.666 3.867
8    119 2.444 2.987

And I have another data set called 'Com' as below:

113 112 113
112 114 119
116 118 119
118 118 119
117 117 119
117 117 119

Using Exp_rem <- lapply(Exp[-1],function(x) structure(x[match(as.matrix ( Com),Exp[,1])],.Dim=dim(Com))) I created a list. Such that Exp_rem$Res is as below:

      [,1]  [,2]  [,3]
[1,] 4.248 7.865 4.248
[2,] 7.865 5.666 2.444
[3,] 7.865 5.666 2.444
[4,] 5.666 5.666 2.444
[5,] 4.248 4.248 2.444
[6,] 4.248 4.248 2.444

And Exp_rem$Ind is as below:

      [,1]  [,2]  [,3]
[1,] 5.666 4.248 5.666
[2,] 4.248 2.444 2.987
[3,] 4.248 3.867 2.987
[4,] 3.867 3.867 2.987
[5,] 6.983 6.983 2.987
[6,] 6.983 6.983 2.987

I am looking to sum through the Exp_rem list to get aggregate values. For eg. 113 will be 4.248 and 4.248(from Exp_rem$Res), 5.666 and 5.666(from Exp_rem$Ind), giving a total of 19.828. The idea is - can we sum across the Exp_rem list as if it were a three dimensional data frame and by creating an object of 113, 114, 115 and so forth - such that the output looks like this:

Loc Value
113 19.828
114 8.11
116 12.113
117 44.924
118 28.599
119 14.935

What is the best way to apply it to a large set data with several lists. Is there a way by not using original Exp data set to produce the values. Thank you.

Sunichie
  • 25
  • 4
  • Please give `dput()` of your list, at least the first rows of these cells mentioned, so that we can produce a solution. See [how to make a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Calum You Jun 20 '18 at 21:37
  • do.call( "+", lapply(lst, function(x) tapply(x, unlist(Com),sum))) – Onyambu Jun 20 '18 at 22:11
  • tapply(unlist(lst),rep(unlist(Com),2),sum) – Onyambu Jun 20 '18 at 22:14

0 Answers0