Programming in R

Sorted by:  

How to Present Dates in Different Formats in R

You’ve probably noticed in R that as.Date() is fairly prescriptive in its defaults: It expects the date to be formatted in the order of year, month, and day. Fortunately, R allows you flexibility in specifying [more…]

How to Add Time Information to Dates in R

Often, referring only to dates in R isn’t enough. There are times when you also need to indicate a specific time in hours and minutes in R.

To specify time information in addition to dates, you can choose [more…]

How to Format and Perform Operations on Dates and Times in R

To format a date for pretty printing in R, you use format(), which takes a POSIXct or POSIXltdatetime as input, together with a formatting string.

Using the example where the object [more…]

How to Extract Specific Elements from a Date in R

Another thing you may want to do in R is to extract specific elements of the date, such as the day, month, or year. For example, scientists may want to compare the weather in a specific month [more…]

How to Go Beyond Date and Time Basics in R

If you only know the basics of how to handle dates and times in R, you may want to explore additional functionality available in R and add-on packages by looking at the following: [more…]

How to Add a Second Dimension in R

In addition to vectors, R can represent matrices as an object you work and calculate with. In fact, R really shines when it comes to matrix calculations and operations.Vectors are closely related to a [more…]

How to Play With Attributes in R

Both the names and the dimensions of matrices and arrays are stored in R as attributes of the object. These attributes can be seen as labeled values you can attach to any object. [more…]

How to Extract Values from a Matrix in R

You can use these indices the same way you use vectors in R. You can assign and extract values, use numerical or logical indices, drop values by using a minus sign, and so forth. [more…]

How to Juggle Dimensions and Replace Values in a Matrix in R

By default, R always tries to simplify the objects to the smallest number of dimensions possible when you use the brackets to extract values from an array. So, if you ask for only one column or row, R [more…]

How to Use Standard Operations in a Matrix in R

Probably the strongest feature of R is its capability to deal with complex matrix operations in an easy and optimized way. Because much of statistics boils down to matrix operations, it’s only natural [more…]

How to Do Matrix Arithmetic in R

Apart from the classical arithmetic operators, R contains a large set of operators and functions to perform a wide set of matrix operations. Many of these operations are used in advanced mathematics, so [more…]

How to Create an Array in R

You have two different options for constructing matrices or arrays. Either you use the creator functions matrix() and array(), or you simply change the dimensions using the [more…]

How to Use Dimensions to Extract Values from an Array in R

Extracting values from an array in R with any number of dimensions is completely equivalent to extracting values from a matrix. You separate the dimension indices you want to retrieve with commas. [more…]

How to Create a Data Frame from Scratch in R

The conversion from a matrix to a data frame in R can’t be used to construct a data frame with different types of values. If you combine both numeric and character data in a matrix for example, everything [more…]

How to Work with Variable Names in R

Variables in a data frame in R always need to have a name. To access the variable names, you can again treat a data frame like a matrix and use the function [more…]

An Overview of the Base Plot Function in R

The base graphics function to create a plot in R is simply called plot(). This powerful function has many options and arguments to control all kinds of things, such as the plot type, line colors, labels [more…]

How to Add Points to a Plot in R

To illustrate some different plot options and types, like points and lines, in R, use the built-in dataset faithful. This is a data frame with observations of the eruptions of the Old Faithful geyser in [more…]

How to Add Lines to a Plot in R

In R, you add lines to a plot in a very similar way to adding points, except that you use the lines()function to achieve this.

But first, use a bit of R magic to create a trend line through the data, called [more…]

How to Create Different Plot Types in R

The plot function in R has a typeargument that controls the type of plot that gets drawn. For example, to create a plot with lines between data points, use [more…]

How to Add Titles and Axis Labels to a Plot in R

You can easily add the main title and axis labels with arguments to the plot() function in R to enhance the quality of your graphic. [more…]

How to Change Plot Options in R

To convey a more powerful and impactful message to the viewer, you can change the look and feel of plots in R using R’s numerous plot options.

You can find all the documentation for changing the look and [more…]

How to Put Multiple Plots on a Single Page in R

To put multiple plots on the same graphics pages in R, you can use the graphics parameter mfrow or mfcol. To use this parameter, you need to supply a vector argument with two elements: the number of rows [more…]

How to Save Graphics to an Image File in R

If you want to publish your results, you have to save your plot to a file in R and then import this graphics file into another document. Much of the time however, you may simply want to use R graphics [more…]

How to Create a Lattice Plot in R

To explore lattice graphics in R, first take a look at the built-in dataset mtcars. This dataset contains 32 observations of motor cars and information about the engine, such as number of cylinders, automatic [more…]

How to Print and Save a Lattice Plot in R

You need to know three essential things about lattice plots in R: how to assign a lattice plot to an object, how to print a lattice plot in a script, and how to save a [more…]

Sign Up for RSS Feeds

Education & Languages