idiomatic

ddply + summarize for repeating same statistical function across large number of columns

北城以北 提交于 2019-11-28 03:07:04
Ok, second R question in quick succession. My data: Timestamp St_01 St_02 ... 1 2008-02-08 00:00:00 26.020 25.840 ... 2 2008-02-08 00:10:00 25.985 25.790 ... 3 2008-02-08 00:20:00 25.930 25.765 ... 4 2008-02-08 00:30:00 25.925 25.730 ... 5 2008-02-08 00:40:00 25.975 25.695 ... ... Basically normally I would use a combination of ddply and summarize to calculate ensembles (e.g. mean for every hour across the whole year). In the case above, I would create a category, e.g. hour (e.g. strptime(data$Timestamp,"%H") -> data$hour and then use that category in ddply , like ddply(data,"hour", summarize,

Idiomatic Scala way to deal with base vs derived class field names?

最后都变了- 提交于 2019-11-28 00:31:54
问题 Consider the following base and derived classes in Scala: abstract class Base( val x : String ) final class Derived( x : String ) extends Base( "Base's " + x ) { override def toString = x } Here, the identifier 'x' of the Derived class parameter overrides the field of the Base class, so invoking toString like this: println( new Derived( "string" ).toString ) returns the Derived value and gives the result "string". So a reference to the 'x' parameter prompts the compiler to automatically

How to idiomatically iterate one half of an array and modify the structure of the other?

做~自己de王妃 提交于 2019-11-27 19:34:27
问题 What is the idiomatic way to iterate (read) over the first half of the vector and change the structure of the second half of the vector depending on the first? This is very abstract but some algorithms could be boiled down to this problem. I want to write this simplified C++ example in Rust: for (var i = 0; i < vec.length; i++) { for (var j = i + 1 ; j < vec.length; j++) { if (f(vec[i], vec[j])) { vec.splice(j, 1); j--; } } } 回答1: An idiomatic solution of this generic problem will be the same

ddply + summarize for repeating same statistical function across large number of columns

霸气de小男生 提交于 2019-11-27 05:04:07
问题 Ok, second R question in quick succession. My data: Timestamp St_01 St_02 ... 1 2008-02-08 00:00:00 26.020 25.840 ... 2 2008-02-08 00:10:00 25.985 25.790 ... 3 2008-02-08 00:20:00 25.930 25.765 ... 4 2008-02-08 00:30:00 25.925 25.730 ... 5 2008-02-08 00:40:00 25.975 25.695 ... ... Basically normally I would use a combination of ddply and summarize to calculate ensembles (e.g. mean for every hour across the whole year). In the case above, I would create a category, e.g. hour (e.g. strptime

When should I use std::size_t?

旧街凉风 提交于 2019-11-26 15:36:42
I'm just wondering should I use std::size_t for loops and stuff instead of int ? For instance: #include <cstdint> int main() { for (std::size_t i = 0; i < 10; ++i) { // std::size_t OK here? Or should I use, say, unsigned int instead? } } In general, what is the best practice regarding when to use std::size_t ? A good rule of thumb is for anything that you need to compare in the loop condition against something that is naturally a std::size_t itself. std::size_t is the type of any sizeof expression and as is guaranteed to be able to express the maximum size of any object (including any array)

When is it appropriate to use an associated type versus a generic type?

别来无恙 提交于 2019-11-26 12:56:57
In this question , an issue arose that could be solved by changing an attempt at using a generic type parameter into an associated type. That prompted the question "Why is an associated type more appropriate here?", which made me want to know more. The RFC that introduced associated types says: This RFC clarifies trait matching by: Treating all trait type parameters as input types , and Providing associated types, which are output types . The RFC uses a graph structure as a motivating example, and this is also used in the documentation , but I'll admit to not fully appreciating the benefits of

Should I use std::size_t or int in my for loops?

佐手、 提交于 2019-11-26 04:29:38
问题 I\'m just wondering should I use std::size_t for loops and stuff instead of int ? For instance: #include <cstdint> int main() { for (std::size_t i = 0; i < 10; ++i) { // std::size_t OK here? Or should I use, say, unsigned int instead? } } In general, what is the best practice regarding when to use std::size_t ? 回答1: A good rule of thumb is for anything that you need to compare in the loop condition against something that is naturally a std::size_t itself. std::size_t is the type of any sizeof

How can I initialise a static Map?

房东的猫 提交于 2019-11-26 00:09:34
问题 How would you initialise a static Map in Java? Method one: static initialiser Method two: instance initialiser (anonymous subclass) or some other method? What are the pros and cons of each? Here is an example illustrating the two methods: import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, String>(); static { myMap.put(1, \"one\"); myMap.put(2, \"two\"); } private static final Map<Integer, String> myMap2 =

How do I reverse an int array in Java?

笑着哭i 提交于 2019-11-25 21:47:45
问题 I am trying to reverse an int array in Java. This method does not reverse the array. for(int i = 0; i < validData.length; i++) { int temp = validData[i]; validData[i] = validData[validData.length - i - 1]; validData[validData.length - i - 1] = temp; } What is wrong with it? 回答1: To reverse an int array, you swap items up until you reach the midpoint, like this: for(int i = 0; i < validData.length / 2; i++) { int temp = validData[i]; validData[i] = validData[validData.length - i - 1];