show empty data points on anychart line graph with dateTime axis - anychart
I have a simple dataset with time series data points that i would like to plot on a per-second accuracy. But the data is ticker data so not every point is represented, only on price change.
Here is a simple example where the price goes 14:08, 14:09, 14:10 ... 14:15
https://playground.anychart.com/ZIT0QuAF/1
Is it possible to display the gap/time passed between data points, either with blank space or probably better the x-axis for each second and just an extended line ( maybe dotted or different colour ) connecting the points.
Thank you.
Now I see your requirements. You can achieve that very easy. As a default and as in the sample you provided, you use the ordinal based scale. To better illustrate that there was some time passed in between ticker points you should use scatter based scale. Just use the following line in your code:
chart.xScale('scatter');
Now all points follow strictly the time coordinate and all gaps between are showed. Also, all points are connected with dotted line. Please, check a sample below
anychart.onDocumentReady(function() {
// create data table on loaded data
var table = anychart.data.table("epoch");
table.addData([
{
last: 0.00000462,
epoch: 1520260206263,
vol: 0,
volBase: 0,
prcnt: 0,
volDiff: 0,
percentFromStart: 0.00000000
},
{
last: 0.00000558,
epoch: 1520260207391,
vol: 0,
volBase: 0,
prcnt: -17.20430108,
volDiff: NaN,
percentFromStart: 20.77922078
},
{
last: 0.00000585,
epoch: 1520260208131,
vol: 0,
volBase: 0,
prcnt: -4.61538462,
volDiff: NaN,
percentFromStart: 26.62337662
},
{
last: 0.00000680,
epoch: 1520260209293,
vol: 0,
volBase: 0,
prcnt: -13.97058824,
volDiff: NaN,
percentFromStart: 47.18614719
},
{
last: 0.00000693,
epoch: 1520260210752,
vol: 0,
volBase: 0,
prcnt: -1.87590188,
volDiff: NaN,
percentFromStart: 50.00000000
},
{
last: 0.00000657,
epoch: 1520260215326,
vol: 0,
volBase: 0,
prcnt: 5.47945205,
volDiff: NaN,
percentFromStart: 42.20779221
},
{
last: 0.00000700,
epoch: 1520260225028,
vol: 0,
volBase: 0,
prcnt: -6.14285714,
volDiff: NaN,
percentFromStart: 51.51515152
},
{
last: 0.00000657,
epoch: 1520260239317,
vol: 0,
volBase: 0,
prcnt: 6.54490107,
volDiff: NaN,
percentFromStart: 42.20779221
},
{
last: 0.00000624,
epoch: 1520260240762,
vol: 0,
volBase: 0,
prcnt: 5.28846154,
volDiff: NaN,
percentFromStart: 35.06493506
},
{
last: 0.00000723,
epoch: 1520260244015,
vol: 0,
volBase: 0,
prcnt: -13.69294606,
volDiff: NaN,
percentFromStart: 56.49350649
},
{
last: 0.00000740,
epoch: 1520260245557,
vol: 0,
volBase: 0,
prcnt: -2.29729730,
volDiff: NaN,
percentFromStart: 60.17316017
},
{
last: 0.00000740,
epoch: 1520260247146,
vol: 0,
volBase: 0,
prcnt: 0.00000000,
volDiff: NaN,
percentFromStart: 60.17316017
},
{
last: 0.00000740,
epoch: 1520260258519,
vol: 0,
volBase: 0,
prcnt: 0.00000000,
volDiff: NaN,
percentFromStart: 60.17316017
},
{
last: 0.00000740,
epoch: 1520260261733,
vol: 0,
volBase: 0,
prcnt: 0.00000000,
volDiff: NaN,
percentFromStart: 60.17316017
},
{
last: 0.00000713,
epoch: 1520260262192,
vol: 0,
volBase: 0,
prcnt: 3.78681627,
volDiff: NaN,
percentFromStart: 54.32900433
},
{
last: 0.00000700,
epoch: 1520260263087,
vol: 0,
volBase: 0,
prcnt: 1.85714286,
volDiff: NaN,
percentFromStart: 51.51515152
},
{
last: 0.00000657,
epoch: 1520260264653,
vol: 0,
volBase: 0,
prcnt: 6.54490107,
volDiff: NaN,
percentFromStart: 42.20779221
},
{
last: 0.00000614,
epoch: 1520260288009,
vol: 0,
volBase: 0,
prcnt: 7.00325733,
volDiff: NaN,
percentFromStart: 32.90043290
},
{
last: 0.00000614,
epoch: 1520260295191,
vol: 0,
volBase: 0,
prcnt: 0.00000000,
volDiff: NaN,
percentFromStart: 32.90043290
},
{
last: 0.00000740,
epoch: 1520260296127,
vol: 0,
volBase: 0,
prcnt: -17.02702703,
volDiff: NaN,
percentFromStart: 60.17316017
},
{
last: 0.00000749,
epoch: 1520260299736,
vol: 0,
volBase: 0,
prcnt: -1.20160214,
volDiff: NaN,
percentFromStart: 62.12121212
},
{
last: 0.00000749,
epoch: 1520260302084,
vol: 0,
volBase: 0,
prcnt: 0.00000000,
volDiff: NaN,
percentFromStart: 62.12121212
},
{
last: 0.00000562,
epoch: 1520260304974,
vol: 0,
volBase: 0,
prcnt: 33.27402135,
volDiff: NaN,
percentFromStart: 21.64502165
},
{
last: 0.00000375,
epoch: 1520260323816,
vol: 0,
volBase: 0,
prcnt: 49.86666667,
volDiff: NaN,
percentFromStart: -18.83116883
},
{
last: 0.00000749,
epoch: 1520260327149,
vol: 0,
volBase: 0,
prcnt: -49.93324433,
volDiff: NaN,
percentFromStart: 62.12121212
},
{
last: 0.00000749,
epoch: 1520260328543,
vol: 0,
volBase: 0,
prcnt: 0.00000000,
volDiff: NaN,
percentFromStart: 62.12121212
},
{
last: 0.00000749,
epoch: 1520260335547,
vol: 0,
volBase: 0,
prcnt: 0.00000000,
volDiff: NaN,
percentFromStart: 62.12121212
},
{
last: 0.00000749,
epoch: 1520260338101,
vol: 0,
volBase: 0,
prcnt: 0.00000000,
volDiff: NaN,
percentFromStart: 62.12121212
}
]);
chart = anychart.stock();
chart.xScale('scatter');
var mappingLastPrice = table.mapAs({
'value': "last"
});
var series1 = chart.plot(0).line(mappingLastPrice);
series1.stroke('red','1', '2 10');
series1.markers(true);
series1.markers().fill('blue');
series1.name("Last Price");
chart.container('container').draw();
});
html, body, #container {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
<script src="https://cdn.anychart.com/releases/8.1.0/js/anychart-base.min.js"></script>
<script src="https://cdn.anychart.com/releases/8.1.0/js/anychart-stock.min.js"></script>
<script src="https://cdn.anychart.com/releases/8.1.0/js/anychart-ui.min.js"></script>
<script src="https://cdn.anychart.com/releases/8.1.0/js/anychart-exports.min.js"></script>
<link href="https://cdn.anychart.com/releases/8.1.0/css/anychart-ui.min.css" rel="stylesheet"/>
<div id="container"></div>
Related
Changing the values of a dictionary Python
In the code below I create a dictionary 3 times and then attempt to change the value of Ontrade from True to False on Strategy 2 with the alteration function. However it does not work as expected and turns all the Ontrade values into True. How would i be able to fix that? import numpy as np import pprint listrange = 5 data = {} dict_integrity = {"Array": np.array([]), "last_timestamp": 0, "last_Trades": 0, "Order_id_list": 0, "Ontrade": False, "TakeProfits": 0, "StopLoss": 0} for x in range(listrange): data["Strategy " + str(x)] = dict_integrity alteration = data["Strategy " + str(2)]["Ontrade"] = True Output: {'Strategy 0': {'Array': array([], dtype=float64), 'Ontrade': True, 'Order_id_list': 0, 'StopLoss': 0, 'TakeProfits': 0, 'last_Trades': 0, 'last_timestamp': 0}, 'Strategy 1': {'Array': array([], dtype=float64), 'Ontrade': True, 'Order_id_list': 0, 'StopLoss': 0, 'TakeProfits': 0, 'last_Trades': 0, 'last_timestamp': 0}, 'Strategy 2': {'Array': array([], dtype=float64), 'Ontrade': True, 'Order_id_list': 0, 'StopLoss': 0, 'TakeProfits': 0, 'last_Trades': 0, 'last_timestamp': 0}} Expected Output: {'Strategy 0': {'Array': array([], dtype=float64), 'Ontrade': False, 'Order_id_list': 0, 'StopLoss': 0, 'TakeProfits': 0, 'last_Trades': 0, 'last_timestamp': 0}, 'Strategy 1': {'Array': array([], dtype=float64), 'Ontrade': False, 'Order_id_list': 0, 'StopLoss': 0, 'TakeProfits': 0, 'last_Trades': 0, 'last_timestamp': 0}, 'Strategy 2': {'Array': array([], dtype=float64), 'Ontrade': True, 'Order_id_list': 0, 'StopLoss': 0, 'TakeProfits': 0, 'last_Trades': 0, 'last_timestamp': 0}}
how to sort the json array by keys in descending order using angular
var arr = { '2021-07-20-21:10': { sends: 1, recvs: 1, notSents: 0, rejects: 0, xptSkips: 0, timeouts: 0, appErrors: 0, responseTimeAvg: 172, responseTimeMax: 172, when: '21:10', hostCount: 1, }, '2021-07-20-21:22': { sends: 1, recvs: 0, notSents: 0, rejects: 0, xptSkips: 0, timeouts: 1, appErrors: 0, responseTimeAvg: 0, responseTimeMax: 0, when: '21:22', hostCount: 1, }, '2021-07-20-21:13': { sends: 2, recvs: 1, notSents: 0, rejects: 0, xptSkips: 0, timeouts: 1, appErrors: 0, responseTimeAvg: 89, responseTimeMax: 177, when: '21:13', hostCount: 2, }, '2021-07-20-21:14': { sends: 1, recvs: 0, notSents: 0, rejects: 0, xptSkips: 0, timeouts: 1, appErrors: 0, responseTimeAvg: 0, responseTimeMax: 0, when: '21:14', hostCount: 1, }} and it has to be sorted based on keys in desc order var arr = { '2021-07-20-21:22': { sends: 1, recvs: 1, notSents: 0, rejects: 0, xptSkips: 0, timeouts: 0, appErrors: 0, responseTimeAvg: 172, responseTimeMax: 172, when: '21:22', hostCount: 1, }, '2021-07-20-21:14': { sends: 1, recvs: 0, notSents: 0, rejects: 0, xptSkips: 0, timeouts: 1, appErrors: 0, responseTimeAvg: 0, responseTimeMax: 0, when: '21:14', hostCount: 1, }, '2021-07-20-21:13': { sends: 2, recvs: 1, notSents: 0, rejects: 0, xptSkips: 0, timeouts: 1, appErrors: 0, responseTimeAvg: 89, responseTimeMax: 177, when: '21:13', hostCount: 2, }, '2021-07-20-21:10': { sends: 1, recvs: 0, notSents: 0, rejects: 0, xptSkips: 0, timeouts: 1, appErrors: 0, responseTimeAvg: 0, responseTimeMax: 0, when: '21:10', hostCount: 1, }}
You can extract the "keys" of your array using the Object.keys() function, then use the sort function to sort that array of keys, and generate a new object iterating over each keys there... Code would be (keeping arr as the name of your array): // Get the keys of your "array" let arrayOfKeys = Object.keys(arr) // We sort it out... let sortedArray = arrayOfKeys.sort() // We will create the "final" array let resultingArray = {} // Now we iterate over each element, in order: sortedArray.forEach((element) => { resultingArray[element] = arr[element] }) ...of course this can be shortened wildly, for example: var resultingArr = {} Object.keys(arr).sort().forEach(e -> { resultingArr[e] = arr[e] }) EDIT: I see that you need to sort the array in descending order. This can be done using a custom sort function, or using the function reverse(). Just correct the sorting step: // We sort it out... let sortedArray = arrayOfKeys.sort().reverse()
Illegal instruction 4 when passing a dynamically allocated array into const
I'm making a 2d array in C dynamically, because I have to populate it with an input file. I populate it and have tested that the values in the array are properly allocated. in = (double**) malloc(trainingSize*sizeof(double*)); for (int i=0; i<trainingSize; i++) { in[i] = (double*) malloc(res*sizeof(double)); } train = (double**) malloc(trainingSize*sizeof(double*)); for (int i=0; i<trainingSize; i++) { train[i] = (double*) malloc(numOutputs*sizeof(double)); for (int j=0; j<numOutputs; j++) { train[i][j] = 0.0; } } The code above seems to be working fine and compiles well, and I was able to properly test that it was working and populated with the correct values. When I try to call this function on it, however, I get an "Illegal instruction: 4" at runtime. genann_train(ann, in[j], train[j], 3); The function definition is void genann_train(genann const *ann, double const *inputs, double const *desired_outputs, double learning_rate); When I test this on a statically allocated array, the program runs just fine. double input[3][400] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},{0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}}; genann_train(ann, input[j], output[j], 3); This is the only change in the code that I made that caused the program to crash.
Not meant as an answer, but to long to post it as comment: I cannot reproduce your runtime error, and actually the way how you allocate your array and how you pass it to genann_train seems to be OK. Maybe you access it in the wrong way in gennan_train in that you exceed array bounds. See the following (slightly modified) code that shows how allocating and using the arrays works with your way of calling the methods: void genann_train(double const *inputs, double const *desired_outputs, double learning_rate) { for (int i=0; i<10; i++) { printf("%lf ", inputs[i]); } } int main() { int trainingSize = 10; int res = 10, numOutputs = 10; double **in = (double**) malloc(trainingSize*sizeof(double*)); for (int i=0; i<trainingSize; i++) { in[i] = (double*) malloc(res*sizeof(double)); } double **train = (double**) malloc(trainingSize*sizeof(double*)); for (int i=0; i<trainingSize; i++) { train[i] = (double*) malloc(numOutputs*sizeof(double)); for (int j=0; j<numOutputs; j++) { train[i][j] = i*j; } } genann_train(train[1], train[1], 3); double input[3][400] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; double output[3][26] = {{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},{0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},{0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}}; genann_train(input[1], output[1], 3); }
Assigning Defined elements to a 2D Array C
I have a 3 large groups of integers that I would like to add to different rows of an array. These integers are defined as follows: #define APARTMENT1_USAGES {0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0.189, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.074, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.111, 0, 0, 0.000, 0, 0, 0, 0, 0, 0.065, 0.167, 0, 0, 0, 0.048, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0} #define APARTMENT2_USAGES {0, 0, 0, 0, 0, 0, 0, 0.130, 0, 0, 0, 0, 0, 0.176, 0, 0.125, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.161, 0.000, 0.039, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0.000, 0.109, 0, 0.032, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.152, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0.135, 0, 0, 0, 0, 0, 0, 0, 0, 0.100, 0, 0, 0.063, 0, 0, 0, 0, 0.000, 0, 0.025, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0.000, 0, 0, 0, 0, 0, 0.378, 0, 0.147, 0.229} #define APARTMENT3_USAGES {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.048, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0.01, 0.01, 0.02, 0.03, 0.03, 0, 0, 0, 0, 0.088, 0, 0} I have shortened these just to show the idea, they are usually 30x24 elements. What I am trying to do is load each of these into a row of a 2D array, but am getting a syntax error without an explanation. I have tried it as follows: double apartmentUsage[3][30*24]; apartmentUsage[1][30*24] = APARTMENT1_USAGES; apartmentUsage[2][30*24] = APARTMENT2_USAGES; apartmentUsage[3][30*24] = APARTMENT3_USAGES;
One of the errors you are facing is as follows: If you define array[3], you can have three values stored in array[0] , array[1] and array[2]. In your code. its defined as apartmentUsage[3][..] but starts with 1. Should be like this- int multiply=30*24; apartmentUsage[0][multiply] = APARTMENT1_USAGES; apartmentUsage[1][multiply] = APARTMENT2_USAGES; apartmentUsage[2][multiply] = APARTMENT3_USAGES; **BUT** this too won't completely solve your problem. Try doing it in the initialization itself as pointed in the comments - double apartmentUsage[3][multiply] = {APARTMENT1_USAGES, APARTMENT2_USAGES, APARTMENT3_USAGES};
How to sum columns with same date in Ruby
I have an array with these type of data inside it, and I need to sum up the columns with same date. [["01-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["02-04-2013", 100.0, 110.0, 130, 0, 0, 0], ["03-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["10-04-2013", 100.0, 110.0, 100, 0, 0, 0], ["02-04-2013", 100.0, 140.0, 0, 70, 0, 0], ["10-04-2013", 100.0, 140.0, 0, 100, 0, 0], ["11-04-2013", 100.0, 140.0, 0, 110, 0, 0], ["12-04-2013", 100.0, 140.0, 0, 120, 0, 0], ["09-04-2013", 0.0, 0.0, 0, 0, 130, 0], ["17-04-2013", 0.0, 0.0, 0, 0, 30, 0], ["15-04-2013", 100.0, 130.0, 0, 0, 0, 17], ["17-04-2013", 100.0, 130.0, 0, 0, 0, 90], ["18-04-2013", 100.0, 130.0, 0, 0, 0, 100]] How can I do it in ruby? I meant to sum up the rows with the same date into one row, and if there is no duplicated date, keep the old ones.
require 'pp' require 'matrix' d = [["01-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["02-04-2013", 100.0, 110.0, 130, 0, 0, 0], ["03-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["10-04-2013", 100.0, 110.0, 100, 0, 0, 0], ["02-04-2013", 100.0, 140.0, 0, 70, 0, 0], ["10-04-2013", 100.0, 140.0, 0, 100, 0, 0], ["11-04-2013", 100.0, 140.0, 0, 110, 0, 0], ["12-04-2013", 100.0, 140.0, 0, 120, 0, 0], ["09-04-2013", 0.0, 0.0, 0, 0, 130, 0], ["17-04-2013", 0.0, 0.0, 0, 0, 30, 0], ["15-04-2013", 100.0, 130.0, 0, 0, 0, 17], ["17-04-2013", 100.0, 130.0, 0, 0, 0, 90], ["18-04-2013", 100.0, 130.0, 0, 0, 0, 100]] pp( d.group_by(&:first).values.reject do |v| v.size <= 1 end.map do |e| e.inject do |m, e| (Vector.[](*m) + Vector.[](*e)).to_a end end ) Update after comments: d.group_by(&:first).values.map do |e| e.inject do |m, e| [e[0], (Vector.[](*m[1..-1]) + Vector.[](*e[1..-1])).to_a].flatten end end.sort Specification change alert: def v m Vector.[](*m.drop(1)) end d.group_by(&:first).values.map do |group| r = group.inject do |m, e| [e[0], *(v(m) + v(e)).to_a] end r[1] /= group.size r[2] /= group.size r end.sort Note. I'm not saying this is homework, but in the cases that are, it should be obvious that when we just do it for the students, we are not really doing them any favors, right? Plus, this solution is provided on a public site that is instantly indexed by google and, being in the top 100 sites in the world, it is not exactly a secret to the prof or the grader. And what if the school is using a national database like http://turnitin.com/ ? I suppose they could check public code snippets if they wanted to. And finally, there is some rather well-written code posted on SO by the, ahem, hobbyists. I'm not sure it can typically pass for lower-division intro-course original work, if I, ahem, say so myself. :-)
aggregated_rows = rows.group_by(&:first).map do |date, rows_by_date| values = rows_by_date.transpose.drop(1).map { |xs| xs.reduce(:+) } [date, values] end #[["01-04-2013", [100.0, 110.0, 120, 0, 0, 0]], # ["02-04-2013", [200.0, 250.0, 130, 70, 0, 0]], ... # ["18-04-2013", [100.0, 130.0, 0, 0, 0, 100]]]
a = [["01-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["02-04-2013", 100.0, 110.0, 130, 0, 0, 0], ["03-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["10-04-2013", 100.0, 110.0, 100, 0, 0, 0], ["02-04-2013", 100.0, 140.0, 0, 70, 0, 0], ["10-04-2013", 100.0, 140.0, 0, 100, 0, 0], ["11-04-2013", 100.0, 140.0, 0, 110, 0, 0], ["12-04-2013", 100.0, 140.0, 0, 120, 0, 0], ["09-04-2013", 0.0, 0.0, 0, 0, 130, 0], ["17-04-2013", 0.0, 0.0, 0, 0, 30, 0], ["15-04-2013", 100.0, 130.0, 0, 0, 0, 17], ["17-04-2013", 100.0, 130.0, 0, 0, 0, 90], ["18-04-2013", 100.0, 130.0, 0, 0, 0, 100]] require "pp" def group_and_sum_rows_by_date_string(a) # instantiate a hash that returns an empty array for a key # that doesn't exist h = Hash.new([]) a.each do |row| # populate the hash with date string as key, and array of # arrays of the values for that date string h[k=row.shift] = ([row] + h[k]).compact end # add up all the corresponding values in each element's array # arrays, and return the result as an array h.map{|k, v| [k, v.transpose.map{|x| x.inject(:+)}]} end pp group_and_sum_rows_by_date_string(a) [["15-04-2013", [100.0, 130.0, 0, 0, 0, 17]], ["03-04-2013", [100.0, 110.0, 120, 0, 0, 0]], ["02-04-2013", [200.0, 250.0, 130, 70, 0, 0]], ["17-04-2013", [100.0, 130.0, 0, 0, 30, 90]], ["18-04-2013", [100.0, 130.0, 0, 0, 0, 100]], ["09-04-2013", [0.0, 0.0, 0, 0, 130, 0]], ["01-04-2013", [100.0, 110.0, 120, 0, 0, 0]], ["12-04-2013", [100.0, 140.0, 0, 120, 0, 0]], ["10-04-2013", [200.0, 250.0, 100, 100, 0, 0]], ["11-04-2013", [100.0, 140.0, 0, 110, 0, 0]]]
require 'pp' a = [["01-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["02-04-2013", 100.0, 110.0, 130, 0, 0, 0], ["03-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["10-04-2013", 100.0, 110.0, 100, 0, 0, 0], ["02-04-2013", 100.0, 140.0, 0, 70, 0, 0], ["10-04-2013", 100.0, 140.0, 0, 100, 0, 0], ["11-04-2013", 100.0, 140.0, 0, 110, 0, 0], ["12-04-2013", 100.0, 140.0, 0, 120, 0, 0], ["09-04-2013", 0.0, 0.0, 0, 0, 130, 0], ["17-04-2013", 0.0, 0.0, 0, 0, 30, 0], ["15-04-2013", 100.0, 130.0, 0, 0, 0, 17], ["17-04-2013", 100.0, 130.0, 0, 0, 0, 90], ["18-04-2013", 100.0, 130.0, 0, 0, 0, 100]] h = {} a.group_by(&:first).each{|k,v| v.flatten!.delete(k); h[k] = v.inject(:+)} pp h output: {"01-04-2013"=>330.0, "02-04-2013"=>650.0, "03-04-2013"=>330.0, "10-04-2013"=>650.0, "11-04-2013"=>350.0, "12-04-2013"=>360.0, "09-04-2013"=>130.0, "17-04-2013"=>350.0, "15-04-2013"=>247.0, "18-04-2013"=>330.0} pp a.group_by(&:first).map{|k,v| v.flatten!.uniq!} Output: [["01-04-2013", 100.0, 110.0, 120, 0], ["02-04-2013", 100.0, 110.0, 130, 0, 140.0, 70], ["03-04-2013", 100.0, 110.0, 120, 0], ["10-04-2013", 100.0, 110.0, 100, 0, 140.0], ["11-04-2013", 100.0, 140.0, 0, 110], ["12-04-2013", 100.0, 140.0, 0, 120], ["09-04-2013", 0.0, 0, 130], ["17-04-2013", 0.0, 0, 30, 100.0, 130.0, 90], ["15-04-2013", 100.0, 130.0, 0, 17], ["18-04-2013", 100.0, 130.0, 0, 100]] pp a.group_by(&:first).map{|k,v| v.transpose.map!{|a| a.inject(:+)}} Output: [["01-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["02-04-201302-04-2013", 200.0, 250.0, 130, 70, 0, 0], ["03-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["10-04-201310-04-2013", 200.0, 250.0, 100, 100, 0, 0], ["11-04-2013", 100.0, 140.0, 0, 110, 0, 0], ["12-04-2013", 100.0, 140.0, 0, 120, 0, 0], ["09-04-2013", 0.0, 0.0, 0, 0, 130, 0], ["17-04-201317-04-2013", 100.0, 130.0, 0, 0, 30, 90], ["15-04-2013", 100.0, 130.0, 0, 0, 0, 17], ["18-04-2013", 100.0, 130.0, 0, 0, 0, 100]]