Hot answers tagged

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 ...
user avatar
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 ...
user avatar
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: ...
user avatar
  • 3,445
16 votes
Accepted

Simulated annealing in R

Here is how I would rewrite your code, comments to follow. ...
user avatar
  • 3,445
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 ...
user avatar
  • 108k
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 ...
user avatar
  • 200
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 ...
user avatar
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: ...
user avatar
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 ...
user avatar
  • 3,445
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 ...
user avatar
  • 1,079
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 ...
user avatar
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 ...
user avatar
  • 393
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 ...
user avatar
6 votes

Generating Cartesian product of strings in R

Well, it can be still useful now-a-days. ...
user avatar
6 votes

Generating Cartesian product of strings in R

You can use expand.grid: ...
user avatar
  • 161
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 ...
user avatar
  • 3,445
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 ...
user avatar
  • 108k
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 ...
user avatar
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: ...
user avatar
  • 76
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 ...
user avatar
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 ...
user avatar
  • 9,171
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 ...
user avatar
  • 3,445
5 votes
Accepted

Merge together multiple dataframes

The for loop can be replaced with a lapply statement. Using base R functions, your ...
user avatar
  • 384
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). ...
user avatar
  • 265
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 <...
user avatar
5 votes
Accepted

Row-wise mean imputation in R

You can use rowMeans with indexing. ...
user avatar
  • 366
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 ...
user avatar
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: ...
user avatar
  • 384
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 ...
user avatar
  • 583
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 ...
user avatar
  • 3,445

Only top scored, non community-wiki answers of a minimum length are eligible