# Programming in R

View:
Sorted by:

### 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.

### 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.

### 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

### 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.

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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:

### 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.

### 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

### 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

### 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

### 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

### 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

### 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

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

### 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.

### 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

### 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.

Listings:1-2526-5051-7576-100more...