Programming in R

Sorted by:  

How to Remove Duplicate Data in R

A very useful application of subsetting data is to find and remove duplicate values. R has a useful function, duplicated(), that finds duplicate values and returns a logical vector that tells you whether [more…]

How to Remove Rows with Missing Data in R

Another useful application of subsetting data frames is to find and remove rows with missing data. The R function to check for this is complete.cases() [more…]

How to Add Calculated Fields to Data in R

After you’ve created the appropriate subset of your data, the next step in your analysis is likely to be to perform some calculations with R. [more…]

How to Create Subgroups of Data in R

The cut() function in R creates bins of equal size (by default) in your data and then classifies each element into its appropriate bin.

If this sounds like a mouthful, don’t worry. A few examples should [more…]

How to Combine and Merge Data Sets in R

You may want to combine data from different sources in your analysis. Generally speaking, you can use R to combine different sets of data in three ways: [more…]

How to Use the merge() Function with Data Sets in R

In R you use the merge() function to combine data frames. This powerful function tries to identify columns or rows that are common between the two different data frames. [more…]

How to Work with Lookup Tables in R

Sometimes doing a full merge of the data in R isn’t exactly what you want. In these cases, it may be more appropriate to match values in a lookup table. To do this, you can use the [more…]

How to Sort and Order Data in R

One very common task in data analysis and reporting is sorting information, which you can do easily in R. You can answer many everyday questions with league tables [more…]

How to Sort Data Frames in R

One way of sorting data in R is to determine the order that elements should be in, if you were to sort. This sounds long winded, but as you’ll see, having this flexibility means you can write statements [more…]

How to Traverse Data with Apply Functions in R

R has a powerful suite of functions that allows you to apply a function repeatedly over the elements of a list. The interesting and crucial thing about this is that it happens without an explicit loop. [more…]

How to Use the apply() Function to Summarize Arrays in R

If you have data in the form of an array or matrix and you want to summarize this data, R’s apply()function is really useful. The apply()function traverses an array or matrix by column or row and applies [more…]

How to Traverse a List or Data Frame with R Apply Functions

When your data is in the form of a list, and you want to perform calculations on each element of that list in R, the appropriate apply function is lapply [more…]

How to Use Apply to Create Tabular Summaries in R

You use tapply() to create tabular summaries of data in R. With tapply(), you can easily create summaries of subgroups in data. This function takes three arguments: [more…]

How to Create a Bar Chart Using ggplot2 in R

To make a bar chart with ggplot2 in R, you use the geom_bar() function. However, note that the default stat is stat_bin(), which is used to cut your data into bins. Thus, the default behavior of [more…]

How to Find Minimum or Maximum Values for Functions in R

A very large body of mathematics aims to solve optimization problems of all kinds. In R, the optimize() function provides one fairly simple mechanism for optimizing functions. [more…]

How to Install and Load CRAN Packages in R

To install a CRAN package in R, use the install.packages() function. This simple command downloads the package from a specified repository (by default, CRAN) and installs it on your machine: [more…]

How to Unload CRAN Packages in R

By loading a package, R first loads the package and then attaches the package and to your search path, which you can see as an internal database that tells R where to find functions and objects. Whenever [more…]

How to Make Scatterplot and Line Charts in R with ggplot2

If you have downloaded and imported ggplot2 for use in your R installation, you can use it to plot your data. To create a scatterplot, you use the geom_point [more…]

How to Plot Summarized Data in a ggplot2 in R

One very convenient feature of ggplot2is its range of functions to summarize your R data in the plot. This means that you often don’t have to pre-summarize your data. For example, the height of bars in [more…]

How to Work with Scales in a ggplot2 in R

In ggplot2 in R, scales control the way your data gets mapped to your geom. In this way, your data is mapped to something you can see (for example, lines, points, colors, position, or shapes). [more…]

How to Add Row and Column Totals in R

One task that you may frequently do in a spreadsheet that you can also do in R is calculating row or column totals. The easiest way to do this is to use the functions [more…]

How to Format Numbers in R

When you produce reports in R, you will want your numbers to appear all nicely formatted to enhance the impact of your data on the viewer. You can use [more…]

How to Use CRAN to Find R Code and Packages

The Comprehensive R Archive Network CRANis a network of web servers around the world where you can find the R source code, R manuals and documentation, and contributed packages. [more…]

How to Install R

You can find the installation files and all necessary information regarding installation on one of the mirror sites of the Comprehensive R Archive Network [more…]

Importing Data into R

R has many functions that allow you to import data from other applications. The following table lists some of the useful text import functions, what they do, and examples of how to use them. [more…]

Sign Up for RSS Feeds

Computers & Software