Questions tagged [reduce]

Reduce refers to the operation, where a array of items is reduced to a single value. Use with language tag like [c++], [google-sheets-formula]. Do NOT use alone.

Documentation in respective languages:

3548 questions
39
votes
5 answers

Java Stream: divide into two lists by boolean predicate

I have a list of employees. They have isActive boolean field. I would like to divide employees into two lists: activeEmployees and formerEmployees. Is it possible to do using Stream API? What is the most sophisticated way?
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
39
votes
3 answers

Understand the `Reduce` function

I have a question about the Reduce function in R. I read its documentation, but I am still confused a bit. So, I have 5 vectors with genes name. For example: v1 <- c("geneA","geneB",""...) v2 <- c("geneA","geneC",""...) v3 <-…
Johnathan
  • 1,877
  • 4
  • 23
  • 29
36
votes
4 answers

How can I merge more than 2 dataframes in R by rownames?

I gather data from 4 df's and would like to merge them by rownames. I am looking for an efficient way to do this. This is a simplified version of the data I have. df1 <- data.frame(N= sample(seq(9, 27, 0.5), 40, replace= T), …
Hans Roelofsen
  • 741
  • 1
  • 7
  • 13
35
votes
1 answer

array.push is not a function - when working with reduce

Can someone please help me understand whats going on here? let firstArray = []; firstArray.push(1); firstArray.push(1); firstArray.push(1); console.log("firstArray", firstArray); // result [ 1, 1, 1 ] - as expected. let secondArray = [1, 2,…
AIon
  • 12,521
  • 10
  • 47
  • 73
35
votes
5 answers

Equivalent of Scala's foldLeft in Java 8

What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce, but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method…
GA1
  • 1,568
  • 2
  • 19
  • 30
35
votes
5 answers

python : can reduce be translated into list comprehensions like map, lambda and filter?

When programming in python, I now avoid map, lambda and filter by using list comprehensions because it is easier to read and faster in execution. But can reduce be replaced as well? E.g. an object has an operator union() that works on another…
Eric H.
  • 2,152
  • 4
  • 22
  • 34
31
votes
4 answers

"Reduce" function for Series

Is there an analog for reduce for a pandas Series? For example, the analog for map is pd.Series.apply, but I can't find any analog for reduce. My application is, I have a pandas Series of lists: >>> business["categories"].head() 0 …
hlin117
  • 20,764
  • 31
  • 72
  • 93
30
votes
2 answers

NameError: global name 'reduce' is not defined

When I try this code in Python 3.x, I get an error message that says "NameError: global name 'reduce' is not defined". def main(): def add(x,y): return x+y reduce(add, range(1, 11)) if __name__=='__main__': main() Why?
anhldbk
  • 4,559
  • 5
  • 29
  • 37
29
votes
3 answers

TypeError: this.reduce is not a function

After adding a method to the Array prototype, some other, unrelated script breaks. [Opera] Unhandled Error: 'this.reduce' is not a function [Firefox] TypeError: this.reduce is not a function The method itself works ([1,2,3].xintsum() outputs 6 as…
user3195057
28
votes
2 answers

Does R have something equivalent to reduce() in Python?

That is : "Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. "
Derrick Zhang
  • 21,201
  • 18
  • 53
  • 73
28
votes
9 answers

Sorting Array with JavaScript reduce function

Often I study some JavaScript interview questions, suddenly I saw a question about usage of reduce function for sorting an Array, I read about it in MDN and the usage of it in some medium articles, But sorting an Array is so Innovative: const arr =…
AmerllicA
  • 29,059
  • 15
  • 130
  • 154
28
votes
3 answers

How to join multiple data frames using dplyr?

I want to left_join multiple data frames: dfs <- list( df1 = data.frame(a = 1:3, b = c("a", "b", "c")), df2 = data.frame(c = 4:6, b = c("a", "c", "d")), df3 = data.frame(d = 7:9, b = c("b", "c", "e")) ) Reduce(left_join, dfs) # a b c d # 1…
nachocab
  • 13,328
  • 21
  • 91
  • 149
27
votes
4 answers

How does the reduce() method work in Java 8?

I try to understand how does the reduce() method work in java-8. For example I have this code: public class App { public static void main(String[] args) { String[] arr = {"lorem", "ipsum", "sit", "amet"}; List strs =…
user9608350
26
votes
2 answers

Why does reduce give a StackOverflowError in Clojure?

I'm trying to concatenate a Seq of Seqs. I can do it with apply concat. user=> (count (apply concat (repeat 3000 (repeat 3000 true)))) 9000000 However, from my limited knowledge, I would assume that the use of apply forces the lazy Seq to be…
Joe
  • 46,419
  • 33
  • 155
  • 245
23
votes
6 answers

python: union keys from multiple dictionary?

I have 5 dictionaries and I want a union of their keys. alldict = [dict1, dict2, dict3, dict4, dict5] I tried allkey = reduce(lambda x, y: set(x.keys()).union(y.keys()), alldict) but it gave me an error AttributeError: 'set' object has no…
Tg.
  • 5,608
  • 7
  • 39
  • 52