reduce

R: How to get the last element from each group?

佐手、 提交于 2019-11-28 14:20:34
I have a data frame containing a time series with two time stamp columns, d$day and d$time , and say, for simplicity, one measured variable d$val1 . Suppose I want to examine the situation at the close of each day's experiment, i.e. the last measurement, if it exists. (Not every day has a measurement, and measurements can be taken at different times each day.) I would like to be able to aggregate by day and use some sort of last() or tail() function on time to pull back the corresponding val . I've tried variations like this with not much success (one issue is that tail requires an argument,

Array reduce function is returning NAN

流过昼夜 提交于 2019-11-28 12:29:28
I have code similar to follows: var temp=[{"name":"Agency","y":32,"drilldown":{"name":"Agency","categories":["APPS & SI","ERS"],"data":[24,8]}},{"name":"ER","y":60,"drilldown":{"name":"ER","categories":["APPS & SI","ERS"],"data":[7,53]}},{"name":"Direct","y":60,"drilldown":{"name":"Direct","categories":["APPS & SI","ERS"],"data":[31,29]}}]; var reduced=temp.reduce(function (a,b) { return a.y + b.y; }); console.log(reduced) // returns NAN You could use a start value and the add only one value from the array. var temp=[{"name":"Agency","y":32,"drilldown":{"name":"Agency","categories":["APPS & SI

Pyspark RDD ReduceByKey Multiple function

无人久伴 提交于 2019-11-28 11:47:44
I have a PySpark DataFrame named DF with (K,V) pairs. I would like to apply multiple functions with ReduceByKey. For example, I have following three simple functions: def sumFunc(a,b): return a+b def maxFunc(a,b): return max(a,b) def minFunc(a,b): return min(a,b) When I apply only one function, e.g,, following three work: DF.reduceByKey(sumFunc) #works DF.reduceByKey(maxFunc) #works DF.reduceByKey(minFunc) #works But, when I apply more than one function, it does not work, e.g., followings do not work. DF.reduceByKey(sumFunc, maxfunc, minFunc) #it does not work DF.reduceByKey(sumFunc, maxfunc)

Java8 stream.reduce() with 3 parameters - getting transparency

与世无争的帅哥 提交于 2019-11-28 11:20:07
I wrote this code to reduce a list of words to a long count of how many words start with an 'A'. I'm just writing it to learn Java 8, so I'd like to understand it a little better [Disclaimer: I realize this is probably not the best way to write this code; it's just for practice!] . Long countOfAWords = results.stream().reduce( 0L, (a, b) -> b.charAt(0) == 'A' ? a + 1 : a, Long::sum); The middle parameter/lambda (called the accumulator) would seem to be capable of reducing the full list without the final 'Combiner' parameter. In fact, the Javadoc actually says: The {@code accumulator} function

Accumulate a Java Stream and only then process it

a 夏天 提交于 2019-11-28 10:31:31
问题 I had a document that looked like the following: data.txt 100, "some text" 101, "more text" 102, "even more text" I processed it using regex and returned a new processed documents as the follow: Stream<String> lines = Files.lines(Paths.get(data.txt); Pattern regex = Pattern.compile("([\\d{1,3}]),(.*)"); List<MyClass> result = lines.map(regex::matcher) .filter(Matcher::find) .map(m -> new MyClass(m.group(1), m.group(2)) //MyClass(int id, String text) .collect(Collectors.toList()); This returns

output of one mapreduce program as input to another mapreduce program

孤人 提交于 2019-11-28 10:15:30
问题 I am trying a simple example, in which the output of one MapReduce job should be the input of another MapReduce job. The flow should be like this: Mapper1 --> Reducer1 --> Mapper2 --> Reducer2 (The output of Mapper1 must be the input of Reducer1. The output of Reducer1 must be the input of Mapper2. The output of Mapper2 must be the input of Reducer2. The output of Reducer2 must be stored in output file). How can I add multiple Mappers and Reducers to my program such that the flow is

Bison Shift/reduce error for C-like language

≡放荡痞女 提交于 2019-11-28 10:02:08
问题 I'm having an issue with my bison grammar, it is giving me shift/reduce errors, and I have already defined precedence for the operators. I know that it is cause by the 'expr binop expr' bit of the expr rule. Here is my bison file, and the output I get. Any help would be very much appreciated. %token ID KEYWORD INTCON FLOATCON TYPE STRING %token IF WHILE FOR VOID RETURN %token AND_OP OR_OP EQ_OP NEQ_OP LEQ_OP GEQ_OP %left OR_OP %left AND_OP %nonassoc '<' LEQ_OP '>' GEQ_OP EQ_OP NEQ_OP %left '+

Is the accumulator of reduce in Java 8 allowed to modify its arguments?

牧云@^-^@ 提交于 2019-11-28 08:23:31
In Java 8, Stream has a method reduce: T reduce(T identity, BinaryOperator<T> accumulator); Is the accumulator operator allowed to modify either of its arguments? I presume not since the JavaDoc says the accumulator should be NonInterfering, though all examples talk of modifying the collection, rather than modifying the elements of the collection. So, for a concrete example, if we have integers.reduce(0, Integer::sum); and suppose for a moment that Integer was mutable, would sum be allowed to modify its first parameter by adding to it (in place) the value of its second parameter? I presume not

Python reduce explanation

拜拜、爱过 提交于 2019-11-28 08:21:13
问题 I'm not able to understand the following code segment: >>> lot = ((1, 2), (3, 4), (5,)) >>> reduce(lambda t1, t2: t1 + t2, lot) (1, 2, 3, 4, 5) How does the reduce function produce a tuple of (1,2,3,4,5) ? 回答1: It's easier if you break out the lambda into a function, so it's clearer to what's going on: >>> def do_and_print(t1, t2): print 't1 is', t1 print 't2 is', t2 return t1+t2 >>> reduce(do_and_print, ((1,2), (3,4), (5,))) t1 is (1, 2) t2 is (3, 4) t1 is (1, 2, 3, 4) t2 is (5,) (1, 2, 3, 4

Hadoop: key and value are tab separated in the output file. how to do it semicolon-separated?

a 夏天 提交于 2019-11-28 07:41:12
I think the title is already explaining my question. I would like to change key (tab space) value into key;value in all output files the reducers are generating from the output of mappers. I could not find good documentation on this using google. Can anyone please give a fraction of code on how to achieve this? Set the configuration property mapred.textoutputformat.separator to ";" In lack of better documentation, here's what I've collected: setTextOutputFormatSeparator(final Job job, final String separator){ final Configuration conf = job.getConfiguration(); //ensure accurate config ref conf