21
votes
Accepted
Benchmarks of various scientific programming languages for theoretical modelling
I started with the Julia code you had and also got ~20 seconds, so I think my timings are similar to yours. Let me give a step by step breakdown on how to do this.
To start, notice that if you are ...
16
votes
Accepted
Generating Cartesian product of strings in R
Your approach is an example of good R code. However, there is a base function that allows creating a cartesian product of strings, interaction. This function ...
16
votes
Accepted
Plot daily time spent on codereview.stackexchange.com
Here is how I would rewrite your code. I will add my comments after:
...
16
votes
Accepted
12
votes
Plot daily time spent on codereview.stackexchange.com
Nice initiative! And R is perfect for this kind of stuff.
Conditional logic
if(!require("lattice")){
install.packages("lattice")
}
require("lattice")
If the ...
10
votes
Fizz, Buzz, or FizzBuzz?
Just to add to @flodel's comments, here is a properly vectorized version. One can see this is quite a bit faster, outperforming the parallel option (on 7 cores) by a factor 5. This problem is a quite ...
8
votes
Accepted
Concise Proof of the Monty Hall Problem in R
The fundamental ideas of your code are solid, however there are a couple of areas that could be improved. For starters, when you call sample and you only want one ...
7
votes
Accepted
Mean of many subsets of a dataframe
Here is a proposal for a different approach that doesn't use a for loop and has some simplifications.
First, an example data frame:
...
7
votes
Accepted
Using Reddit API in R
Here are my main recommendations:
use recursion
use names instead of list indices, for example node$data$reply$data$children reads much better than ...
7
votes
Accepted
Rolling regressions in R
For each group in your data table, your code computes the coefficient b1 from a linear regression y = b0 + b1*x + epsilon, and ...
7
votes
Accepted
Moving average function in C++ for use with R
Overall your code is not bad, but we can do better. Let's start with the coding style.
Your variables are clearly named and your code is easy to follow, however the use of ...
7
votes
Truth table generator for an arbitrary function
The main flaw that can be observed in your function is the presence of code duplication: expressions such as 2^numArguments and ...
6
votes
Accepted
Vector comparison inside for-loop
The following function works without for loops or any function of the *apply family. Furthermore, it does not require additional ...
6
votes
6
votes
6
votes
Accepted
Calculation of mortgage repayments
I spent clearly too much time on this... Main ideas:
your understanding of how the payment on a fixed mortgage is computed was wrong. The payment is computed once at the beginning and remains the ...
6
votes
Accepted
Largest common multiple equal or lower than max
Looping is generally not recommended in R.
And it's not really needed here.
As @vnp pointed out in a comment,
all you need is simply calculate the least common multiple of the values in the input ...
6
votes
Benchmarks of various scientific programming languages for theoretical modelling
in your MATLAB code, some unnecessary steps can be skipped. Here's a modification of your doStep function that should improve performance quite a bit. Each modif is ...
6
votes
Accepted
More concise way to sum elements of columns in R
Assuming your data are numeric and your columns are ordered by D-#, the following approaches should work:
Using apply:
...
6
votes
Accepted
Merge dataframes on timestamps and time intervals using data.table in R
You'll need to use by.x to get around setting x's key.
Also, note that foverlaps can merge ...
6
votes
Speeding up string splitting in Rcpp/Cpp
Remove unnecessary steps
for (int i = 0; i < field_number - 1; ++i)
{ /*read discard*/ }
double value;
// read the needed value now
This will make sure that ...
5
votes
Accepted
Identify changes between two data.frames, explain deltas for x columns
I feel you wrote some very complicated code when the hardest part (from an algorithm point of view) should be a single merge of your two data.frames. So my rewrite is centered around a call to the ...
5
votes
Accepted
Merge together multiple dataframes
The for loop can be replaced with a lapply statement.
Using base R functions, your ...
5
votes
Accepted
Using doParallel to cycle through *.rds files
Parallelization is not necessarily implemented nicely in R. However, it is far more ideal to use R's batch process than opening 10x Rstudio sessions as you saw (less of a resource drain per task).
...
5
votes
Accepted
Permutations with repetitions algorithm in R
This is one of many instances where people would be tempted to say, "see, loops in R are slow". And my response is, "loops aren't that slow, but you can put slow code in loops."
There's no reason <...
5
votes
Accepted
5
votes
Benchmarks of various scientific programming languages for theoretical modelling
Regarding the rpois function, I think the preferred way of simulating a single value from a Poisson distribution with mean λ is
...
5
votes
Accepted
A simple function to rename columns in data frame to standardized names in R
Most R functions are vectorized, you don't need the for loop:
...
5
votes
Accepted
Fizz, Buzz, or FizzBuzz?
To answer your initial questions:
I can't speak for R good practices in general - but I like
functions, and I'm still reusing many of my functions created in my
first R script. They also work well ...
5
votes
Accepted
Binary search in R
First, let's point out that %in% and match do not require that the input be sorted so they should asymptotically (i.e as the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
r × 399performance × 129
statistics × 37
matrix × 35
beginner × 32
algorithm × 19
simulation × 16
data-visualization × 16
c++ × 15
time-limit-exceeded × 13
vectorization × 12
rcpp × 11
strings × 9
datetime × 9
functional-programming × 9
machine-learning × 9
regex × 8
combinatorics × 8
clustering × 8
csv × 7
bioinformatics × 7
geospatial × 7
vectors × 5
memory-optimization × 5
python × 4