Questions tagged [sum]

The result of adding two or more quantities together.

The sum or total refers to the result of a summation operation. To refer to the operation yields a sum, use the [tag: addition] tag. A summation involves adding two or more numbers or other quantities. It is primarily an arithmetic operation which means it deals with numbers and also other mathematical objects. However, sum can be used in other contexts which may not necessarily be a result of a mathematical operation. Almost all programming languages support addition of numbers using the + operator. Most programming languages support the summation operation through a built-in language construct or as a utility function part of the standard library.


Examples of sum in various programming languages

C++

#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<int> array{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int sum = std::accumulate(array.begin(), array.end(), 0, std::plus<int>());
    std::cout << sum;
    return 0;
}

Python:

sum_of_items = sum([1, 2, 3, 4, 5, 6])
# sum_of_items will be equal to 21

SQL

SELECT SUM(column1) FROM MyTable
WHERE column1 = "condition"

C#

List<int> array = new List<int>() {1, 2, 3, 4, 5};
int sum = array.Sum();  # sum == 21

Java

import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
      var numbers = Arrays.asList(1, 2, 3, 4, 5);
      var sum = numbers.stream().mapToInt(Integer::intValue).sum();
      System.out.println(sum);
    }
}

R

x <- c(1.1, 2.5, -0.4)
y <- c(NA, 1.7)
sum(x)
sum(y)
sum(y, na.rm = TRUE)
sum(x, y)  ## as same as sum(c(x, y))
sum(x, y, na.rm = TRUE)  ## as same as sum(c(x, y), na.rm = TRUE)
15351 questions
2
votes
3 answers

How to sum up the word count for each person in a dialogue?

I'm starting to learn Python and I'm trying to write a program that would import a text file, count the total number of words, count the number of words in a specific paragraph (said by each participant, described by 'P1', 'P2' etc.), exclude these…
epo3
  • 2,991
  • 2
  • 33
  • 60
2
votes
1 answer

Gsheet - Arrayformula function to include 2 conditions (AND operator)

The goal is to create an arrayformula that looks over two separate columns and returns a SUM if it matches a certain string. Here's an example table: Feature Status Description API Completed Lorem ipsum Database In review lorem…
2
votes
2 answers

Summing, Lookups and multiple sheets in Excel

I'm not a native Excel user (much more of a SQL man) and I have the following scenario that is doing my head in. Mainly because I'm sure it's relatively simple, but because I'm not super-familiar with all the advanced functions of Excel. I have a 2…
TheDemonLord
  • 327
  • 1
  • 2
  • 15
2
votes
1 answer

Sum two values on the previous row

I have two columns in my dataframe: "Cust_ID" and "Fill_days". I need to add a column for "Adjusted Days", like this: The idea: for every first row for "Cust_ID", the "Adjusted Days" value should be 0. For the following rows, if previous row's…
Rishabh
  • 178
  • 9
2
votes
1 answer

How to sum two objects?

I want to do an application that pareses text. So far, I have a class called Result, that holds the value and type each part of an equation. public enum ResultType { Int32, Double, Boolean, Color, DateTime, String, …
Marco Bruggmann
  • 605
  • 2
  • 12
  • 23
2
votes
2 answers

Apache Pig Sum calculation

I am trying to calculate the total shares but am receiving an error with the code, what am I missing? grunt> history 1 stockprice = load 'PG/TutorialA/input/stockprice.csv' using PigStorage(',') AS(Stock:chararray,price:int); 2 investor = load…
Wing
  • 21
  • 2
2
votes
2 answers

Creating a new column that has the sum of multiple columns per row using data.table in R

I have a data.table in R that's shown below: I want to add a column that represents the sum of ace, bb, and statin for every row. So for the rows shown above, the new column would have the following values: 2,1,2,1,2 The name of the table above is…
amatof
  • 175
  • 1
  • 13
2
votes
3 answers

Calculate a sum on a range in column in pandas

I would like to calculate a sum on a range in column in panda dataframe in python. Panda DataFrame: acc accname amount 1010 turnover 10000 2000 salaries -4000 3000 rent -1500 5000 assets 15000 6000 …
2
votes
2 answers

calculator exercise is not returning the correct output

I am really new at learning JavaScript and I am completing some exercises from the Odin Project. I am stuck at one part of the calculator that should sum all the arguments, but the output changes if you use an array to call the function. My code is…
osvasas
  • 23
  • 3
2
votes
2 answers

Rowwise sum is not working in dplyr for R

Dataset I have simulated this dataset for my question: #### Set Seed #### set.seed(123) #### Create Data Frame #### df <- data.frame(x1 = rbinom(n=100, size=1, prob = .5), …
Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30
2
votes
2 answers

sum select rows based on condition tidyverse

I can't find an answer to this exact question. Using tidyverse, I would like to sum the values of rows that contain a value >= 1 into a new row. I have: df <- tibble(dif = c(0, 1, 2, 3), n = c(2, 4, 6, 8)) dif n 1 0 …
jnat
  • 63
  • 6
2
votes
1 answer

Is there a way to produce a sum for rows below in a pandas dataframe?

I have a dataframe and I want to produce a column that shows the sum of all the rows below and including each row... for example: A B 2 10 i.e 2+4+3+1 4 8 i.e 4+3+1 3 4 i.e 3+1 1 1 I have data in column 'A' and then I…
SHV_la
  • 875
  • 1
  • 10
  • 14
2
votes
2 answers

Vectorizing MATLAB function

I have double summation over m = 1:M and n = 1:N for polar point with coordinates rho, phi, z: I have written vectorized notation of it: N = 10; M = 10; n = 1:N; m = 1:M; rho = 1; phi = 1; z = 1; summ = cos (n*z) * besselj(m'-1, n*rho) *…
N0rbert
  • 559
  • 5
  • 22
2
votes
1 answer

Google Sheet - calculating attendance from moving date range

Good day folks, we have a Google Sheet with training attendance. We are meeting on weekly basis and calculate attendance from the past four months, last 36 month and historical total. Situation: Currently, we calculate the attendance by assigning 2…
2
votes
1 answer

SUM multiple columns by different condition from same table and then group by date

I tried to SUM values of multiple columns (CASH+CARD+CHEQUE+REST) and group them to display totals for each day. Actual…
Delta
  • 23
  • 3
1 2 3
99
100