# Tag Info

## Hot answers tagged r

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 ...
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 ...
Accepted

### Plot daily time spent on codereview.stackexchange.com

Here is how I would rewrite your code. I will add my comments after: ...
• 3,445
Accepted

### Simulated annealing in R

Here is how I would rewrite your code, comments to follow. ...
• 3,445

### 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 ...
• 108k

### 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 ...
• 200
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 ...
• 431
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: ...
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 ...
• 3,445
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 ...
• 1,079
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 ...
• 431

### 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 ...
• 393
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 ...

### Generating Cartesian product of strings in R

Well, it can be still useful now-a-days. ...

### Generating Cartesian product of strings in R

You can use expand.grid: ...
• 161
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 ...
• 3,445
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 ...
• 108k

### 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 ...
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: ...
• 76
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 ...

### 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 ...
• 9,171
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 ...
• 3,445
Accepted

### Merge together multiple dataframes

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

### Row-wise mean imputation in R

You can use rowMeans with indexing. ...
• 366

### 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 ...
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: ...
• 384
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 ...
• 583