Programming in R

Sorted by:  

How to Test Data Normality in a Formal Way in R

The graphical methods for checking data normality in R still leave much to your own interpretation. There’s much discussion in the statistical world about the meaning of these plots and what can be seen [more…]

How to Convert a Factor in R

Sometimes you need to explicitly convert factors to either text or numbers. To do this, you use the functions as.character() or as.numeric(). First, convert your [more…]

How to Look at the Structure of a Factor in R

R has a special data structure for categorical data, called factors. Factors are closely related to characters because any character vector can be represented by a factor. To look a little bit under the [more…]

How to Distinguish Data Types in R

In the field of statistics, being able to distinguish between variables of different types is very important. The type of data very often determines the type of analysis that can be performed. As a result [more…]

How to Work with Ordered Factors in R

In R, there is a special data type for ordinal data. This type is called ordered factors and is an extension of factors that you’re already familiar with. [more…]

How to Do More with Loops in R

R contains some of the mechanisms used in other programming languages to manipulate loops: [more…]

How to Apply Functions to a Vector in R

The apply() function works on anything that has dimensions in R, but what if you don’t have dimensions? For that, you have two related functions from the apply family at your disposal [more…]

How to Simplify Results (or Not) with the sapply Function in R

The sapply() function doesn’t always return a vector. In fact, the standard output of sapply is a list, but that list gets simplified to either a matrix or a vector [more…]

How to Use the Dots Argument in R

There are multiple ways to add arguments in R. The addPercent() function rounds every percentage to one decimal place, but you can add another argument to specify the number of digits the [more…]

How to Search for Multiple Words in R

When working with text in R, you may need to find words or patterns inside text. Imagine you have a list of the states in the United States, and you want to find out which state names consist of two words [more…]

How to Substitute Text in R

The sub() function (short for substitute) in R searches for a pattern in text and replaces this pattern with replacement text. You use sub() to substitute text for text, and you use its cousin [more…]

How to Extend Text Functionality with Stringr in R

If you’ve worked at all with the text manipulation functions of R, you probably wonder why all these functions have such unmemorable names and seemingly diverse syntax. If so, you’re not alone. [more…]

How to Use Regular Expressions in R

R supports the concept of regular expressions, which allows you to search for patterns inside text. You may never have heard of regular expressions, but you’re probably familiar with the broad concept. [more…]

How to Create a Factor in R

To create a factor in R, you use the factor() function. The first three arguments of factor() warrant some exploration: [more…]

How to Create a Function in R

To create a function in R, you will make and transform an R script. The best way to learn to swim is by jumping in the deep end, so let’s just write a function to show you how easy that is in R. [more…]

How to Use a Function in R

Once you have created and transformed a script in R, save the script again, and load it into the console using the source() command displayed earlier. Now you see . . . nothing. R doesn’t let you know [more…]

How to Reduce the Number of Lines in R

When transforming the script in R, not all elements are required. In fact, the return() statement is optional, because, by default, R will always return the value of the last line of code in the function [more…]

How to Use Arguments When You Transform a Script in R

There are multiple ways to use arguments in R. Here are some ways you can specify arguments in a function call when transforming a script. [more…]

How to Work with Files and Folders in R

You know how to import your data into R and export your data from R. Now all you need is an idea of where the files are stored with R and how to manipulate those files. Every R session has a default location [more…]

How to Manipulate Files in R

Occasionally, you may want to write a script in R that will traverse a given folder and perform actions on all the data in the files or a subset of files in that folder. [more…]

How to Compare Two Data Samples with R’s T-Test

If you want to use R’s t.test()function to compare your data, you first have to check, among other things, whether both samples are normally distributed. [more…]

How to Compare Two Data Samples with R

R gives you two standard tests for comparing two groups with numerical data: the t-test with the t.test() function, and the Wilcoxon test with the wilcox.test [more…]

How to Compare Paired Data with R

When testing differences between two data groups in R, you can have either paired or unpaired data. Paired data comes from experiments where two different treatments were given to the same subjects. [more…]

How to Test Data Proportions with R

Let’s look at an example to illustrate the basic R tests for data proportions. The following example is based on real research, published by Robert Rutledge, MD, and his colleagues in the [more…]

How to Analyze Data in Tables with R

You can use R’s prop.test()function for data in matrices and tables. For prop.test(), these tables need to have two columns with the number of counts for the two possible outcomes. [more…]


Sign Up for RSS Feeds

Computers & Software