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
2 answers

R - sum multiple separate columns for each unique ID? Aggregate?

My dataset features several blocks, each containing several plots. In each plot, three different lifeforms were marked as present/absent (i.e.…
Millet
  • 21
  • 1
2
votes
2 answers

How to sum the value of 2 rows with vlookup by only using 1 formula?

Here is what I did =VLOOKUP(M3,P2:Q23,2,FALSE)+VLOOKUP(N3,P2:Q23,2,FALSE) I want to sum the values with just one formula and not repeat it Im using Excel Online I tried =XLOOKUP(M2:N2,P3:P23,Q3:Q23) but I get a value error,does anyone know how to…
MochA
  • 23
  • 3
2
votes
3 answers

How to use Java streams and reduce() to get the sum of calculations with the key and value of the Map?

I'll try to keep it as brief as possible. I hava this map: Map connections. It contains a Neuron-Objekt as Key and the weight of the connection as value. The Neuron class has a method "getOutput" to get the output-Value of the…
Wewius
  • 87
  • 7
2
votes
4 answers

Multiplying Values from two different Dictionaries

I have two dictionaries which I need to multiply and get the total of, both with the same keys (I need the total of the stock and price of some items). # Create a list "menu" with 4 items menu = ["sandwich", "burger", "fish", "chips"] # Create a…
mandykg
  • 33
  • 4
2
votes
2 answers

Numpy Sum Array Coordinates to Array efficiently

Given an Array of Points points = np.array([[1,1,1], [0,1,1], [1,0,0], [1,1,1]]) create an array that counts the coordinates, meaning: r = [[0,0], [[1,0], [0,1]], [0,2]] meaning the coordinate [1,1,1] exists twice so a the position [1,1,1]…
erikgo
  • 31
  • 4
2
votes
1 answer

Select only rows which the SUM of specific column is distinct to zero with group by another colum SQL Server

I want to filter a table eliminating the rows that give zero in a sum over an "amount" column and that are grouped by an Id, for example, if I have "-10, 10 and 15" for the same Id. I want to eliminate the rows that have -10 and 10 and stay with row…
2
votes
1 answer

How to selects items by argument and sums them in another function

Hi I basically have example how Write a program to track family income and expenses. So I need to 2 functions one that adds items into seznam ok I did that. 2 functions that selects items by argument and sums them. How to add second function…
2
votes
2 answers

Partially aggregate (sum) values that are below the first n observations in dplyr

I have a dataframe like df: id <- c("aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj") value <- c(10, 9, 8, 7, 6, 5, 4, 3, 2, 1) df <- data.frame(id, value) I want to aggregate the values that are rank below n-th position (in this case,…
vog
  • 770
  • 5
  • 11
2
votes
3 answers

Sum based on column condition

I am trying to sum sales on my table grouped for next 30 days. Here is the data I…
2
votes
1 answer

Google Sheets: How do I format a range to color a row if every cell in that row has matching data?

I want to make it so if all of the cells in a row has data that matches, it will automatically change the row's fill color to green. Example of the desired visual effect, including my conditional formatting work-around to achieve the look As you can…
2
votes
1 answer

Google Sheet yields infinitesimal number as remainder of an integer/whole number

I have this worksheet where I need to create a checker to determine whether a number (result of dividing the sum of two numbers by another value --DIVISOR) is an integer/does not have decimals. Upon running the said checker, it mostly worked just…
2
votes
3 answers

Find all combinations that add up to given number python with list of lists

I've seen plenty of threads on how to find all combinations that add up to a number with one list, but wanted to know how to expand this such that you can only pick one number at a time, from a list of lists Question: You must select 1 number from…
Curious Student
  • 156
  • 1
  • 8
2
votes
3 answers

xslt 1 and sum function

I create an invoice management system using xml and PHP but the latest PHP version does not support XSLT 2.0 - therefore I have to look for an sum() alternative. How can I transform the valid xslt 2 function "sum(unitprice * quantity)" to xslt 1.0 ?…
NaN
  • 3,501
  • 8
  • 44
  • 77
2
votes
2 answers

Consolidating and summing columns in R with similar names

I need some help consolidating columns in R I have ~130 columns, some of which have a similar name. For example, I have ~25 columns called "pathogen". However, after importing my datasheet into R, these colums are now listed as follows :…
pradoer
  • 29
  • 5
2
votes
3 answers

How to sum consecutive equal digits in a number in Java

The following question was asked in my last interview (yesterday), and I'm trying to solve it since then (couldn't solve it in the interview). Sorry for any grammar mistakes or any logical mistakes, I don't have the question, it was written by…
NoobCoder
  • 513
  • 3
  • 18