Does R have Increment operators like += in C? (And How To Make One)

Incrementing in a for loop or a while loop is a very common thing that you’ll do when writing code. These incrementors allow you to track your loop, so there are many different instances when you’ll potentially be using an incrementor. Sometimes, you’ll need to keep track of the number of lines in a file or tell your code to stop at a certain point. In those cases, an incrementor will come in very handy for your shiny code. If you have used other languages such as C or Python, then you’re likely very familiar with the += operator.

The short answer is that R does not have an increment operator – such as the ++ operator in C++ or Java. However, that doesn’t mean that you’re not able to create an incrementor on your own. It’s operators are limited to a number of mathematical operators: including addition, subtraction, modulo, inner product, outer product, and others.

The Easy Way

The great thing about working with R is that you can create your own incrementor. Furthermore, it’s simple to do this using regular mathematical operators. The simplest choice for being able to create an incrementor for your R code is by setting a standard variable equal to zero and adding 1 each time the loop passes. Suppose X is our variable, we would assign it to zero as we would normally assign a value to a variable in r: x <- 0. Then, we would assign a +1 value (or whichever value you would need to increment by) upon each instance of the loop: x <- x + 1.

A Simple and Efficient Solution

R does have the seq() function. This is a simple and efficient solution, and the seq function takes three different values. The three basic arguments for this function are from, to, and by. The function is in the form of seq(from, to, by= ). The from and to arguments are used to initiate and stop the sequence. These two alone are enough to initiate the sequence, with a default incrementor of 1. The by= argument can be set to another value if you need to increment by another value. This function by default returns a list if it’s added to a variable.

Alternative Using ROperators Packages

It’s not entirely true that R doesn’t have an incrementor. It’s just that an increment function is not built into R as it is with other languages. If you’re slightly concerned about the speed of your code, you may want to consider using the ROperators package. After installing the ROperators package, this offers a incrementor function in the format of %+=%. In the X example above, this would be x %+=% 1. The package can use other mathematical operators as well, including: subtraction, multiplication, division and exponent. In place of the plus sign, you would replace the plus operator with the sign of your choice: -, *, /, or ^, respectively. Here is a link to the documentation for this package.

Looping through an array

Do you need to loop through a list of values with your R code and need to increment each time with your loop? Your best option in this case is to use the addition operator. Just add a single 1 value to the variable.
Here’s an example:
Letters <- list(“a”, “b”, “c”, “d”)
Count <- 0
for (q in Letters) {
count <- count + 1
}

With the ROperators package, you’re simply replacing the assignment variable and the plus sign with the %+=% operator. It’s similar to the ++ operator which you’re familiar with, but you have to install the ROperators package to gain access to this operator.

Require(roperators)
Letters <- list(“a”, “b”, “c”, “d”)
Count <- 0
for (q in Letters) {
count %+=% 1
}
ROperators Vs Increment
The biggest advantage of using the ROperators function over adding one to the variable is performance. If you’re working with a large amount of data, then you’ll be constantly pushing numbers in and out of memory with the x <- x + 1 incrementor.

Example: Working with multiple text files
Suppose that you’re looping through a directory of files and you’d like to keep track of which file your code is working on. You’ll probably want to use this number in your code later on.
As normal, you would use the list.files() function to load in the names of your files into a variable that are located in your current working directory.
fileList <- list.files()
You’re going to use a for loop to loop through each file one by one:
For (i in fileList) {
}
You would want to create a variable to store the file count and initialize it to zero:
fileCount <- 0
Then use the incrementor of your choice to keep track of the number of files in your directory/
With the roperators package, your code would look like this:

Require(roperators)
fileCount <- 0
For (i in fileList) {
fileCount %+=% 1
}
Print(fileCount)
With the standard +1 method, your code would look like this:
fileCount <- 0
For (i in fileList) {
fileCount <- fileCount + 1
}
Print(fileCount)

Scroll to top
Privacy Policy