**View:**

**Sorted by:**

### How to Name the Values in Your Vectors in R

Often the elements of vectors are referred to by their positions — that is, x[5] refers to the fifth element in vector x. One very powerful feature in R, however, gives names to the elements of a vector [more…]

### How to Create and Assign Named Vectors in R

You use the assignment operator (<-) to assign names to vectors in much the same way that you assign values to character vectors.

Imagine you want to create a named vector with the number of days in each [more…]

### How to Split Strings in R

A collection of combined letters and words is called a *string.* Whenever you work with text, you need to be able to concatenate words (string them together) and split them apart. In R, you use the [more…]

### How to Concatenate Text Cases in R

You can put together separate data elements in R so that they form a single text string. To concatenate text, you use the paste() function: [more…]

### How to Sort Text Cases in R

Data can be sorted alphabetically or numerically, in ascending or descending order. Like any programming language, R makes it easy to compile lists of sorted and ordered data. [more…]

### How to Search for Individual Words in R

When you’re working with text, often you can solve problems if you’re able to find words or patterns inside text. R makes this easy to do. Imagine you have a list of the states in the United States, and [more…]

### How to Use Functions as Arguments in R

In R, you can pass a function itself as an argument. You can easily assign the complete code of a function to a new object. Similarly, you also can assign the function code to an argument. This opens up [more…]

### How to Match Functions in R

When using functions as arguments in R, it is sometimes necessary to match functions. This also means that if you have an object with the same name as the function you want to use, this whole construct [more…]

### How to Cross the Borders in R

While working with functions in R, sometimes, you work with some objects that you didn’t first create in the workspace. You use the arguments x, mult, and [more…]

### How to Use Internal Functions in R

At times, it can be very helpful to use internal functions in R. Writing your functions in such a way that they need objects in the global environment doesn’t really make sense, because you use functions [more…]

### How to Dispatch to a Method in R

A useful tool when dealing with functions in R is dispatching to a method. One thing to remember about functions in R is that one function can give a different result based on the type of value you give [more…]

### How to Adapt the addPercent Function in R

Suppose you want to be able to paste the percent sign to character vectors with the addPercentfunction. A character vector will give an error the moment you try to multiply it, so you need another function [more…]

### How to Use If Statements in R

If statements can be very useful in R, as they are in any programming language,. Often, you want to make choices and take action dependent on a certain value. [more…]

### How to Use If…Else Statements in R

When using R, sometimes you need your function to do something if a condition is true and something else if it is not. You could do this with two if statements, but there’s an easier way in R: an [more…]

### How to Use Vectorization with If Statements in R

Vectorization is one of the defining attributes of the R language. R wouldn’t be R if it didn’t have some kind of vectorized version of an if...elsestatement. [more…]

### How to Switch Between Possibilities with If…Else Statements in R

At times, it is useful to switch between possibilities in R. The nested if...else statement is especially useful if you have complete code blocks that have to be carried out when a condition is met. But [more…]

### How to Loop Through Values in R

Sometimes when making choices using R, you can use only a single value to base your choice on. You could apply that code on each value you have by hand, but it makes far more sense to automate this task [more…]

### How to Use Loops with Indices in R

Using loops in R is very handy, but you can write more efficient code if you loop not over the values but over the indices. To do so, you replace the middle section in the function with the following code [more…]

### How to Apply Functions on Rows and Columns in R

In R, you can use the apply() function to apply a function over every row or column of a matrix or data frame. This presents some very handy opportunities. [more…]

### How to Use Factors or Numeric Data in R

Before you attempt to describe your data in R, you have to make sure your data is in the right format. This means [more…]

### How to Count Unique Data Values in R

To figure out what data can be factored when working in R, let’s take a look at the dataset mtcars. This built-in dataset describes fuel consumption and ten different design points from 32 cars from the [more…]

### How to Prepare Data in R

With R at your fingertips, you can quickly shape your data exactly as you want it. That’s good because in many real-life cases, you get heaps of data in a big file, and preferably in a format you can’t [more…]

### How to Describe the Center of Continuous Data in R

You have the dataset and you’ve formatted it to fit your needs in R, so now you’re ready for the real work. Analyzing your data always starts with describing it. This way you can detect errors in the data [more…]

### How to Describe the Variation of Data in R

A single number doesn’t tell you much about your data. Often it’s as important to know the spread of your data. You can use R to look at this spread using a number of different approaches. [more…]

### How to Use Data Tables in R

A first step in every analysis, using R or not, consists of calculating the descriptive statistics for your dataset. You have to get to know the data you received before you can accurately decide what [more…]