# Programming in R

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

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

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

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

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

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

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

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

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

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

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

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

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