Programming in R

Sorted by:  

How to Use Arithmetic Vector Operations in R

One set of arithmetic functions in R consists of functions in which the outcome is dependent on more than one value in the vector. Summing a vector with the [more…]

The Benefits of Using R

Of the many attractive benefits of R programming language, a few are easy to recognize. It’s actively maintained, it has good connectivity to various types of data and other systems, and it’s versatile [more…]

How to Perform Multiple Calculations with Vectors Using R

R is a vector-based language. You can think of a vectoras a row or column of numbers or text. The list of numbers {1,2,3,4,5}, for example, could be a vector. Unlike most other programming languages, R [more…]

How to Repeat Vectors in R

In R, you can combine a vector with itself if you want to repeat it, but if you want to repeat the values in a vector many times, using the c() function becomes a bit impractical. R makes life easier by [more…]

How to Construct Vectors in R

A vector is the simplest type of data structure in R. The R manual defines a vector as “a single entity consisting of a collection of things.” A collection of numbers, for example, is a numeric vector [more…]

How to Store and Calculate Values in R

Using R as a calculator is very interesting but perhaps not all that useful. A much more useful capability is storing values and then doing calculations on these stored values. [more…]

How to Vectorize Your Functions in R

Vectorized functions are a very useful feature of R, but programmers who are used to other languages often have trouble with this concept at first. A vectorized [more…]

How to Put Arguments in Your R Functions

Most functions in R have arguments that give them more information about exactly what you want them to do. If you use print("Hello world!"), you give the argument [more…]

How to Install, Load, and Unload Packages in R

You install a package in R with the function — wait for it — install.packages(). Who could’ve guessed? So, to install the fortunes package, for example, you simply give the name of the package as a string [more…]

How to Do Basic Arithmetic in R

Since R is statistics platform, it has a rather complete set of arithmetic operators, so you can use R as a fancy calculator if the need arises. [more…]

How to Round Off Numbers in R

Although R can calculate accurately to up to 16 digits, you don’t always want to use that many digits. In this case, you can use a couple functions in R to round numbers. To round a number to two digits [more…]

How to Use Mathematical Functions in R

In R, of course, you want to use more than just basic operators. R comes with a whole set of mathematical functions. R naturally contains a whole set of functions that you’d find on a technical calculator [more…]

How to Change Values in a Vector in R

Changing values in a vector in R is actually pretty simple. To illustrate, let’s assume that you created two vectors containing the number of baskets that Granny and Geraldine made in six basketball games [more…]

How to Use Trigonometric Functions in R

All trigonometric functions are available in R: the sine, cosine, and tangent functions and their inverse functions. You can find them on the Help page you reach by typing [more…]

How R Calculates Infinite, Undefined, and Missing Values

Luckily, R can deal with data anomalies that confound some other statistical platforms. For instance, in some cases, you don’t have real values to calculate with. In most real-life data sets, in fact, [more…]

How to Compare Values in Logical Vectors in R

To build logical vectors in R, you’d better know how to compare values, and R contains a set of operators that you can use for this purpose. [more…]

How to Use Logical Vectors as Indices in R

The index function in R doesn’t take only numerical vectors as arguments; it also works with logical vectors. You can use these logical vectors very efficiently to select some values from a vector. [more…]

How to Look at the Structure of Vectors in R

A vector is a one-dimensional set of values, all the same type. It’s the smallest unit you can work with in R. A single value is technically a vector as well — a vector with only one element. In mathematics [more…]

How to Test Vector Types in R

R contains a set of functions that allow you to test for the type of a vector. All these functions have the same syntax: is, a dot, and then the name of the type. [more…]

How to Create Vectors in R

To create a vector from a simple sequence of integers, for example, you use the colon operator (:) in R. The code 3:7 gives you a vector with the numbers 3 to 7, and [more…]

How to Combine Logical Statements in R

Life would be boring in R if you couldn’t combine logical statements. If you want to test whether a number lies within a certain interval, for example, you want to check whether it’s greater than the lowest [more…]

How to Summarize Logical Vectors in R

If you use logical values in arithmetic operations, R sees TRUE as 1 and FALSE as 0. This allows for some pretty interesting constructs.

To illustrate, let’s assume you have two vectors containing the number [more…]

How to Get Lists with the lapply Function in R

The lapply() function works exactly the same as the sapply() function, with one important difference: It always returns a list. This trait can be beneficial if you’re not sure what the outcome of [more…]

How to Create Character Vectors for Text Data in R

Text in R is represented by character vectors. A character vector is — you guessed it! — a vector consisting of characters.

In the world of computer programming, text often is referred to as a [more…]

How to Extract a Subset of a Vector in R

You use the same indexing rules for character vectors that you use for numeric vectors (or for vectors of any type). The process of referring to a subset of a vector through indexing its elements is also [more…]

Sign Up for RSS Feeds

Education & Languages