63

This is an example of what I need to do:

var myarray = [5, 10, 3, 2];

var result1 = myarray[0];
var result2 = myarray[1] + myarray[0];
var result3 = myarray[2] + myarray[1] + myarray[0];
var result4 = myarray[3] + myarray[2] + myarray[1] + myarray[0];

so all that would output 5, 15, 18, 20

but instead of writing out all the vars like that, I want it to say something like:

var result = arrayitem + the sum of any previous items 

Does that make sense? Is that possible? How do I do that?

0

31 Answers 31

1
2
0

I was looking for how to do cumulative sums within groupBy in using @tidyjs/tidy. Here is what works for me:

// Here is how to do a cumSum using tidy.js starting with unsorted data
async function runAsync() {

    // Dynamic import ESM @tidyjs/tidy in this CommonJS env
    const tidy = await import('https://cdn.jsdelivr.net/npm/@tidyjs/[email protected]/+esm');
    const { tidy: tidyFn, groupBy, sort, mutateWithSummary, roll, n, sum } = tidy;

    const data = [
        {str: 'foo', value: 3},
        {str: 'bar', value: 3},
        {str: 'foo', value: 1},
        {str: 'bar', value: 1},
        {str: 'bar', value: 7},
    ];
    const test = tidyFn(data,
        groupBy(['str'], [
                mutateWithSummary({
                    cumsum: roll(n(), sum('value'))
                })
            ]
        ));
    console.log("test: " + JSON.stringify(test));
}

// Here is how to do a cumSum using tidy.js starting with unsorted data
async function runAsync() {

    // Dynamic import ESM @tidyjs/tidy in this CommonJS env
    const tidy = await import('https://cdn.jsdelivr.net/npm/@tidyjs/[email protected]/+esm');
    const { tidy: tidyFn, groupBy, sort, mutateWithSummary, roll, n, sum } = tidy;

    const data = [
        {str: 'foo', value: 3},
        {str: 'bar', value: 3},
        {str: 'foo', value: 1},
        {str: 'bar', value: 1},
        {str: 'bar', value: 7},
    ];
    const test = tidyFn(data,
        groupBy(['str'], [
                mutateWithSummary({
                    cumsum: roll(n(), sum('value'))
                })
            ]
        ));
    console.log("test: " + JSON.stringify(test));
}

runAsync().catch(err => console.error('oops', err));

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, Borewit, but I get an error when I try to run it in Stack Snippets: { "message": "SyntaxError: import declarations may only appear at top level of a module", "filename": "stacksnippets.net/js", "lineno": 13, "colno": 1 }
[deleted]
[deleted]
Sorry. I could not get the async import to work.
Hi Peter, I made your code is now runnable. I deleted my previous comments as they are no longer relevant.
Thanks, Borewit. That is instructive.
Peter, you broke the code by removing the last line: runAsync().catch(err => console.error('oops', err))
|
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.