Subsetting data consists on obtaining a subsample of the original data, in order to obtain specific elements based on some condition. In this tutorial you will learn in detail **how to make a subset in R** in the most common scenarios, explained with several examples.

### How to subset data in R?

**Subsetting data in R can be achieved by different ways**, depending on the data you are working with. In general, you can subset:

**Using square brackets**( and operators).- Using the
**dollar sign**( ) if the elements are named. **With functions**, like the command for conditional or logical subsets.

### Single and double square brackets in R

Before the explanations for each case, it is worth to mention the **difference between using single and double square brackets** when subsetting data in R, in order to avoid explaining the same on each case of use. Suppose you have the following named numeric vector:

As we will explain in more detail in its corresponding section, you could access the first element of the vector using single or with double square brackets and specifying the index of the element.

The difference is that **single square brackets will maintain the original input structure** but the **double will simplify it** as much as possible. This can be verified with the following example:

Other interesting characteristic is when you try to **access observations out of the bounds** of the vector. In this case, if you use **single square brackets** you will obtain a value but **an error with double brackets**.

However, sometimes it is not possible to use double brackets, like working with data frames and matrices in several cases, as it will be pointed out on its corresponding sections.

Note that when subsetting gives no observations means that you are trying to subset under some condition that never meets.

### Subset function in R

The function allows **conditional subsetting in R** for vector-like objects, matrices and data frames.

In the following sections we will use both this function and the operators to the most of the examples. Note that this function allows you to subset by one or multiple conditions.

### Subset vector in R

**Subsetting a variable in R** stored in a vector can be achieved in several ways:

- Selecting the
**indices**you want to display. If more than one, select them using the function. - Using
**boolean indices**to indicate if a value must be selected () or not (). - Using
**logical operators**with the function. - If you want to select all the values except one or some, make a subset indicating the index with negative sign.

The following summarizes the ways to subset vectors in R with several examples.

is useful when you want to assign the same value to all the elements of a already created vector. As an example, will replace all the values of the vector with 1, but will override the vector as a number.

In addition, if your vector is named, you can use the previous and the following ways to subset the data, specifying the elements name as character.

Note that vectors can be of any data type.

### Subsetting a list in R

Consider the following sample list:

You can subset the list elements with single or double brackets to subset the elements and the subelements of the list.

In case you have a list with names, you can access them specifying the element name or accessing them with the dollar sign.

In addition, it is also possible to make a **logical subsetting in R for lists**. For example, you could replace the first element of the list with a subset of it in the following way:

### Subset R data frame

Subsetting a data frame consists on obtaining some rows or columns of the full data frame, or some that meet one or several conditions. It is very usual to subset a data frame in R for analysis purposes. Consider, for instance, the following sample data frame:

### Columns subset in R

You can **subset a column in R** in different ways:

- If you want to subset just one column, you can use single or double square brackets to specify the index or the name (between quotes) of the column.
- Specifying the indices after a comma (leaving the first argument blank selects all rows of the data frame). In this case you can’t use double square brackets, but use argument.
- In case of subsetting multiple columns of a data frame just indicate the columns inside a vector.

The following block of code shows some examples:

When subsetting more than one column or when specifying rows and columns (using a comma inside brackets) you will need to set to maintain the original structure of the object, instead of using double square brackets.

#### Subset dataframe by column name

**Subsetting dataframe using column name in R** can also be achieved using the dollar sign (), specifying the name of the column with or without quotes.

#### Subset dataframe by column value

You can also subset a data frame depending on the values of the columns. As an example, you may want to make a subset with all values of the data frame where the corresponding value of the column is greater than 5, or where the group of the column is Group 1.

Note that when subsetting a data frame by column value you have to specify the condition in the first argument, as the output will be a subset of rows of the data frame.

You can also apply a **conditional subset** by column values with the function as follows. Note that when using this function you can use the variable names directly.

When using the function with a data frame you can also **specify the columns you want to be returned**, indicating them in the argument.

In adition, you can use multiple subset conditions at once. **Subsetting with multiple conditions** is just easy as subsetting by one condition. In the following example we select the values of the column , where the value is 1 or where it is 6.

### Subset rows in R

Analogously to column subset, you can subset rows of a data frame indicating the indices you want to subset as the first argument between square brackets.

#### Subset rows by list of values

In case you want to subset rows based on a vector you can use the operator or the function as follows:

If you need the opposite of you can create new operator with or write .

#### Subset by date

Many data frames have a column of dates. In this case, each row represents a date and each column an event registered on those dates. For this purpose, you need to transform that column of dates with the function to convert the column to date format.

As an example, you can subset the values corresponding to dates greater than January, 5, 2011 with the following code:

#### Subsetting in R by unique date

Note that in case your date column contains the same date several times and you want to select all the rows that correspond to that date, you can use the logical operator with the function as follows:

### Subset a matrix in R

Subsetting a matrix in R is very similar to subsetting a data frame. Consider the following sample matrix:

You can subset the rows and columns specifying the indices of rows and then of columns. You can also use boolean data type.

Note that if you subset the matrix to just one column or row it will be converted to a vector. In order to preserve the matrix class, you can set the argument to .

### Subset matrix by column and row names

In case your matrix contains row or column names, you can use them instead of the index to subset the matrix. In the following example we selected the columns named ‘two’ and ‘three’.

### Subset matrix by column values

Equivalently to data frames, you can subset a matrix by the values of the columns. In this case, we are making a subset based on a condition over the values of the third column.

### Subset time series

Time series are a type of R object with which you can create subsets of data based on time. We will use, for instance, the time series.

The function allows you to create subsets of time series, as shown in the following example:

## subset: Subsetting Vectors, Matrices and Data Frames

### Description

Return subsets of vectors, matrices or data frames which meet conditions.

### Usage

subset(x, …)# S3 method for default subset(x, subset, …)

# S3 method for matrix subset(x, subset, select, drop = FALSE, …)

# S3 method for data.frame subset(x, subset, select, drop = FALSE, …)

### Arguments

subset

logical expression indicating elements or rows to keep: missing values are taken as false.

select

expression, indicating columns to select from a data frame.

drop

passed on to indexing operator.

…

further arguments to be passed to or from other methods.

### Value

An object similar to contain just the selected elements (for a vector), rows and columns (for a matrix or data frame), and so on.

### Warning

This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like , and in particular the non-standard evaluation of argument can have unanticipated consequences.

### Details

This is a generic function, with methods supplied for matrices, data frames and vectors (including lists). Packages and users can add further methods.

For ordinary vectors, the result is simply .

For data frames, the argument works on the rows. Note that will be evaluated in the data frame, so columns can be referred to (by name) as variables in the expression (see the examples).

The argument exists only for the methods for data frames and matrices. It works by first replacing column names in the selection expression with the corresponding column numbers in the data frame and then using the resulting integer vector to index the columns. This allows the use of the standard indexing conventions so that for example ranges of columns can be specified easily, or single columns can be dropped (see the examples).

The argument is passed on to the indexing method for matrices and data frames: note that the default for matrices is different from that for indexing.

Factors may have empty levels after subsetting; unused levels are not automatically removed. See for a way to drop all unused levels from a data frame.

### See Also

,

### Examples

# NOT RUN { subset(airquality, Temp > 80, select = c(Ozone, Temp)) subset(airquality, Day == 1, select = -Temp) subset(airquality, select = Ozone:Wind) with(airquality, subset(Ozone, Temp > 80)) ## sometimes requiring a logical 'subset' argument is a nuisance nm <- rownames(state.x77) start_with_M <- nm %in% grep("^M", nm, value = TRUE) subset(state.x77, start_with_M, Illiteracy:Murder) # but in recent versions of R this can simply be subset(state.x77, grepl("^M", nm), Illiteracy:Murder) # }## Subsetting

You’re reading the first edition of Advanced R; for the latest on this topic, see the Subsetting chapter in the second edition.

R’s subsetting operators are powerful and fast. Mastery of subsetting allows you to succinctly express complex operations in a way that few other languages can match. Subsetting is hard to learn because you need to master a number of interrelated concepts:

The three subsetting operators.

The six types of subsetting.

Important differences in behaviour for different objects (e.g., vectors, lists, factors, matrices, and data frames).

The use of subsetting in conjunction with assignment.

This chapter helps you master subsetting by starting with the simplest type of subsetting: subsetting an atomic vector with . It then gradually extends your knowledge, first to more complicated data types (like arrays and lists), and then to the other subsetting operators, and . You’ll then learn how subsetting and assignment can be combined to modify parts of an object, and, finally, you’ll see a large number of useful applications.

Subsetting is a natural complement to . shows you the structure of any object, and subsetting allows you to pull out the pieces that you’re interested in.

##### Quiz

Take this short quiz to determine if you need to read this chapter. If the answers quickly come to mind, you can comfortably skip this chapter. Check your answers in answers.

What is the result of subsetting a vector with positive integers, negative integers, a logical vector, or a character vector?

What’s the difference between , , and when applied to a list?

When should you use ?

If is a matrix, what does do? How is it different to ?

How can you use a named vector to relabel categorical variables?

##### Outline

Data types starts by teaching you about . You’ll start by learning the six types of data that you can use to subset atomic vectors. You’ll then learn how those six data types act when used to subset lists, matrices, data frames, and S3 objects.

Subsetting operators expands your knowledge of subsetting operators to include and , focussing on the important principles of simplifying vs. preserving.

In Subsetting and assignment you’ll learn the art of subassignment, combining subsetting and assignment to modify parts of an object.

Applications leads you through eight important, but not obvious, applications of subsetting to solve problems that you often encounter in a data analysis.

### Data types

It’s easiest to learn how subsetting works for atomic vectors, and then how it generalises to higher dimensions and other more complicated objects. We’ll start with , the most commonly used operator. Subsetting operators will cover and , the two other main subsetting operators.

### Atomic vectors

Let’s explore the different types of subsetting with a simple vector, .

Note that the number after the decimal point gives the original position in the vector.

There are five things that you can use to subset a vector:

**Positive integers**return elements at the specified positions:**Negative integers**omit elements at the specified positions:You can’t mix positive and negative integers in a single subset:

**Logical vectors**select elements where the corresponding logical value is . This is probably the most useful type of subsetting because you write the expression that creates the logical vector:If the logical vector is shorter than the vector being subsetted, it will be

*recycled*to be the same length.A missing value in the index always yields a missing value in the output:

**Nothing**returns the original vector. This is not useful for vectors but is very useful for matrices, data frames, and arrays. It can also be useful in conjunction with assignment.**Zero**returns a zero-length vector. This is not something you usually do on purpose, but it can be helpful for generating test data.

If the vector is named, you can also use:

**Character vectors**to return elements with matching names.

### Lists

Subsetting a list works in the same way as subsetting an atomic vector. Using will always return a list; and , as described below, let you pull out the components of the list.

### Matrices and arrays

You can subset higher-dimensional structures in three ways:

- With multiple vectors.
- With a single vector.
- With a matrix.

The most common way of subsetting matrices (2d) and arrays (>2d) is a simple generalisation of 1d subsetting: you supply a 1d index for each dimension, separated by a comma. Blank subsetting is now useful because it lets you keep all rows or all columns.

By default, will simplify the results to the lowest possible dimensionality. See simplifying vs. preserving to learn how to avoid this.

Because matrices and arrays are implemented as vectors with special attributes, you can subset them with a single vector. In that case, they will behave like a vector. Arrays in R are stored in column-major order:

You can also subset higher-dimensional data structures with an integer matrix (or, if named, a character matrix). Each row in the matrix specifies the location of one value, where each column corresponds to a dimension in the array being subsetted. This means that you use a 2 column matrix to subset a matrix, a 3 column matrix to subset a 3d array, and so on. The result is a vector of values:

### Data frames

Data frames possess the characteristics of both lists and matrices: if you subset with a single vector, they behave like lists; if you subset with two vectors, they behave like matrices.

### S3 objects

S3 objects are made up of atomic vectors, arrays, and lists, so you can always pull apart an S3 object using the techniques described above and the knowledge you gain from .

### S4 objects

There are also two additional subsetting operators that are needed for S4 objects: (equivalent to ), and (equivalent to ). is more restrictive than in that it will return an error if the slot does not exist. These are described in more detail in the OO field guide.

### Exercises

Fix each of the following common data frame subsetting errors:

Why does yield five missing values? (Hint: why is it different from ?)

What does return? How does subsetting a matrix with it work? Do we need any additional subsetting rules to describe its behaviour?

Why does return an error? How does it differ from the similar ?

Implement your own function that extracts the diagonal entries from a matrix (it should behave like where is a matrix).

What does do? How does it work?

### Subsetting operators

There are two other subsetting operators: and . is similar to , except it can only return a single value and it allows you to pull pieces out of a list. is a useful shorthand for combined with character subsetting.

You need when working with lists. This is because when is applied to a list it always returns a list: it never gives you the contents of the list. To get the contents, you need :

“If list is a train carrying objects, then is the object in car 5; is a train of cars 4-6.”

— @RLangTip

Because it can return only a single value, you must use with either a single positive integer or a string:

Because data frames are lists of columns, you can use to extract a column from data frames: , .

S3 and S4 objects can override the standard behaviour of and so they behave differently for different types of objects. The key difference is usually how you select between simplifying or preserving behaviours, and what the default is.

### Simplifying vs. preserving subsetting

It’s important to understand the distinction between simplifying and preserving subsetting. Simplifying subsets returns the simplest possible data structure that can represent the output, and is useful interactively because it usually gives you what you want. Preserving subsetting keeps the structure of the output the same as the input, and is generally better for programming because the result will always be the same type. Omitting when subsetting matrices and data frames is one of the most common sources of programming errors. (It will work for your test cases, but then someone will pass in a single column data frame and it will fail in an unexpected and unclear way.)

Unfortunately, how you switch between simplifying and preserving differs for different data types, as summarised in the table below.

Vector | ||

List | ||

Factor | ||

Array | or | or |

Data frame | or | or |

Preserving is the same for all data types: you get the same type of output as input. Simplifying behaviour varies slightly between different data types, as described below:

**Atomic vector**: removes names.**List**: return the object inside the list, not a single element list.**Factor**: drops any unused levels.**Matrix**or**array**: if any of the dimensions has length 1, drops that dimension.**Data frame**: if output is a single column, returns a vector instead of a data frame.

is a shorthand operator, where is equivalent to . It’s often used to access variables in a data frame, as in or .

One common mistake with is to try and use it when you have the name of a column stored in a variable:

There’s one important difference between and . does partial matching:

If you want to avoid this behaviour you can set the global option to . Use with caution: it may affect behaviour in other code you have loaded (e.g., from a package).

### Missing/out of bounds indices

and differ slightly in their behaviour when the index is out of bounds (OOB), for example, when you try to extract the fifth element of a length four vector, or subset a vector with or :

The following table summarises the results of subsetting atomic vectors and lists with and and different types of OOB value.

OOB | ||

OOB | Error | Error |

Error | ||

Error | Error |

If the input vector is named, then the names of OOB, missing, or components will be .

### Exercises

- Given a linear model, e.g., , extract the residual degrees of freedom. Extract the R squared from the model summary ()

### Subsetting and assignment

All subsetting operators can be combined with assignment to modify selected values of the input vector.

Subsetting with nothing can be useful in conjunction with assignment because it will preserve the original object class and structure. Compare the following two expressions. In the first, will remain as a data frame. In the second, will become a list.

With lists, you can use subsetting + assignment + to remove components from a list. To add a literal to a list, use and :

### Applications

The basic principles described above give rise to a wide variety of useful applications. Some of the most important are described below. Many of these basic techniques are wrapped up into more concise functions (e.g., , , ), but it is useful to understand how they are implemented with basic subsetting. This will allow you to adapt to new situations that are not dealt with by existing functions.

### Lookup tables (character subsetting)

Character matching provides a powerful way to make lookup tables. Say you want to convert abbreviations:

If you don’t want names in the result, use to remove them.

### Matching and merging by hand (integer subsetting)

You may have a more complicated lookup table which has multiple columns of information. Suppose we have a vector of integer grades, and a table that describes their properties:

We want to duplicate the info table so that we have a row for each value in . We can do this in two ways, either using and integer subsetting, or and character subsetting:

If you have multiple columns to match on, you’ll need to first collapse them to a single column (with , , or ). You can also use or , which do the same thing for you — read the source code to see how.

### Random samples/bootstrap (integer subsetting)

You can use integer indices to perform random sampling or bootstrapping of a vector or data frame. generates a vector of indices, then subsetting to access the values:

The arguments of control the number of samples to extract, and whether sampling is performed with or without replacement.

### Ordering (integer subsetting)

takes a vector as input and returns an integer vector describing how the subsetted vector should be ordered:

To break ties, you can supply additional variables to , and you can change from ascending to descending order using . By default, any missing values will be put at the end of the vector; however, you can remove them with or put at the front with .

For two or more dimensions, and integer subsetting makes it easy to order either the rows or columns of an object:

More concise, but less flexible, functions are available for sorting vectors, , and data frames, .

### Expanding aggregated counts (integer subsetting)

Sometimes you get a data frame where identical rows have been collapsed into one and a count column has been added. and integer subsetting make it easy to uncollapse the data by subsetting with a repeated row index:

### Removing columns from data frames (character subsetting)

There are two ways to remove columns from a data frame. You can set individual columns to NULL:

Or you can subset to return only the columns you want:

If you know the columns you don’t want, use set operations to work out which colums to keep:

### Selecting rows based on a condition (logical subsetting)

Because it allows you to easily combine conditions from multiple columns, logical subsetting is probably the most commonly used technique for extracting rows out of a data frame.

Remember to use the vector boolean operators and , not the short-circuiting scalar operators and which are more useful inside if statements. Don’t forget De Morgan’s laws, which can be useful to simplify negations:

- is the same as
- is the same as

For example, simplifies to , and then to .

is a specialised shorthand function for subsetting data frames, and saves some typing because you don’t need to repeat the name of the data frame. You’ll learn how it works in non-standard evaluation.

### Boolean algebra vs. sets (logical & integer subsetting)

It’s useful to be aware of the natural equivalence between set operations (integer subsetting) and boolean algebra (logical subsetting). Using set operations is more effective when:

You want to find the first (or last) .

You have very few s and very many s; a set representation may be faster and require less storage.

allows you to convert a boolean representation to an integer representation. There’s no reverse operation in base R but we can easily create one:

Let’s create two logical vectors and their integer equivalents and then explore the relationship between boolean and set operations.

When first learning subsetting, a common mistake is to use instead of . Here the achieves nothing: it switches from logical to integer subsetting but the result will be exactly the same. In more general cases, there are two important differences. First, when the logical vector contains NA, logical subsetting replaces these values by NA while drops these values. Second, is **not** equivalent to : if is all FALSE, will be and is still , so you’ll get no values, instead of all values. In general, avoid switching from logical to integer subsetting unless you want, for example, the first or last value.

### Exercises

How would you randomly permute the columns of a data frame? (This is an important technique in random forests.) Can you simultaneously permute the rows and columns in one step?

How would you select a random sample of rows from a data frame? What if the sample had to be contiguous (i.e., with an initial row, a final row, and every row in between)?

How could you put the columns in a data frame in alphabetical order?

### Answers

Positive integers select elements at specific positions, negative integers drop elements; logical vectors keep elements at positions corresponding to ; character vectors select elements with matching names.

selects sub-lists. It always returns a list; if you use it with a single positive integer, it returns a list of length one. selects an element within a list. is a convenient shorthand: is equivalent to .

Use if you are subsetting a matrix, array, or data frame and you want to preserve the original dimensions. You should almost always use it when subsetting inside a function.

If is a matrix, will replace every element with 0, keeping the same number of rows and columns. completely replaces the matrix with the value 0.

A named character vector can act as a simple lookup table:

[This article was first published on ** (R)very Day**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Often, when you’re working with a large data set, you will only be interested in a small portion of it for your particular analysis. So, how do you sort through all the extraneous variables and observations and extract only those you need? Well, R has several ways of doing this in a process it calls “subsetting.”

The most basic way of subsetting a data frame in R is by using square brackets such that in:

example[x,y]

example is the data frame we want to subset, ‘x’ consists of the rows we want returned, and ‘y’ consists of the columns we want returned. Let’s pull some data from the web and see how this is done on a real data set.

### import education expenditure data set and assign column names education <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/robustbase/education.csv", stringsAsFactors = FALSE) colnames(education) <- c("X","State","Region","Urban.Population","Per.Capita.Income","Minor.Population","Education.Expenditures") View(education)Here’s what the first part of our data set looks like after I’ve imported the data and appropriately named its columns.

Now, let’s suppose we oversee the Midwestern division of schools and that we are charged with calculating how much money was spent per child for each state in our region. We would need three variables: State, Minor.Population, and Education.Expenditures. However, we would only need the observations from the rows that correspond to Region 2. Here’s the basic way to retrieve that data in R:

ed_exp1 <- education[c(10:21),c(2,6:7)]To create the new data frame ‘ed_exp1,’ we subsetted the ‘education’ data frame by extracting rows 10-21, and columns 2, 6, and 7. Pretty simple, right?

Another way to subset the data frame with brackets is by omitting row and column references. Take a look at this code:

ed_exp2 <- education[-c(1:9,22:50),-c(1,3:5)]Here, instead of subsetting the rows and columns we wanted returned, we subsetted the rows and columns we did not want returned and then omitted them with the “-” sign. If we now call ed_exp1 and ed_exp2, we can see that both data frames return the same subset of the original education data frame.

Now, these basic ways of subsetting a data frame in R can become tedious with large data sets. You have to know the exact column and row references you want to extract. It’s pretty easy with 7 columns and 50 rows, but what if you have 70 columns and 5,000 rows? How do you find which columns and rows you need in that case? Here’s another way to subset a data frame in R…

ed_exp3 <- education[which(education$Region == 2),names(education) %in% c("State","Minor.Population","Education.Expenditures")]Now, we have a few things going on here. First, we are using the same basic bracketing technique to subset the education data frame as we did with the first two examples. This time, however, we are extracting the rows we need by using the which() function. This function returns the indices where the Region column of the education data from is 2. That gives us the rows we need. We retrieve the columns of the subset by using the %in% operator on the names of the education data frame.

Now, you may look at this line of code and think that it’s too complicated. There’s got to be an easier way to do that. Well, you would be right. There is another basic function in R that allows us to subset a data frame without knowing the row and column references. The name? You guessed it: subset().

ed_exp4 <- subset(education, Region == 2, select = c("State","Minor.Population","Education.Expenditures"))The subset() function takes 3 arguments: the data frame you want subsetted, the rows corresponding to the condition by which you want it subsetted, and the columns you want returned. In our case, we take a subset of education where “Region” is equal to 2 and then we select the “State,” “Minor.Population,” and “Education.Expenditure” columns.

When we subset the education data frame with either of the two aforementioned methods, we get the same result as we did with the first two methods:

Now, there’s just one more method to share with you. This last method, once you’ve learned it well, will probably be the most useful for you in manipulating data. Let’s take a look at the code and then we’ll go over it…

install.packages("dplyr") library(dplyr) ed_exp5 <- select(filter(education, Region == 2),c(State,Minor.Population:Education.Expenditures))This last method is not part of the basic R environment. To use it, you’ve got to install and download the dplyr package. If you’re going to be working with data in R, though, this is a package you will definitely want. It is among the most downloaded packages in the R environment and, as you start using it, you’ll quickly see why.

So, once we’ve downloaded dplyr, we create a new data frame by using two different functions from this package:

**filter**: the first argument is the data frame; the second argument is the condition by which we want it subsetted. The result is the entire data frame with only the rows we wanted.**select**: the first argument is the data frame; the second argument is the names of the columns we want selected from it. We don’t have to use the names() function, and we don’t even have to use quotation marks. We simply list the column names as objects.

In this example, we’ve wrapped the filter function in the selection function to return our data frame. In other words, we’ve first taken the rows where the Region is 2 as a subset. Then, we took the columns we wanted from only those rows. The result gives us a data frame consisting of the data we need for our 12 states of interest:

So, to recap, here are 5 ways we can subset a data frame in R:

- Subset using brackets by extracting the rows and columns we want
- Subset using brackets by omitting the rows and columns we don’t want
- Subset using brackets in combination with the which() function and the %in% operator
- Subset using the subset() function
- Subset using the filter() and select() functions from the dplyr package

That’s it! Happy subsetting!

*Related*

## In r subset

## Subsetting Data

R has powerful indexing features for accessing object elements. These features can be used to select and exclude variables and observations. The following code snippets demonstrate ways to keep or delete variables and observations and to take random samples from a dataset.

### Selecting (Keeping) Variables

To practice this interactively, try the selection of data frame elements exercises in the Data frames chapter of this introduction to R course.

### Excluding (DROPPING) Variables

### Selecting Observations

### Selection using the Subset Function

The **subset( ) **function is the easiest way to select variables and observations. In the following example, we select all rows that have a value of age greater than or equal to 20 or age less then 10. We keep the ID and Weight columns.

In the next example, we select all men over the age of 25 and we keep variables weight *through* income (weight, income and all columns between them).

To practice the **subset()** function, try this this interactive exercise. on subsetting data.tables.

### Random Samples

Use the **sample( )** function to take a **random sample of size n** from a dataset.

Sours: https://www.statmethods.net/management/subset.html

The investigator that the victim should come in. And here it is, I just could not resist such a worthy offer. I don't think I offended you either, you liked it yourself, especially that. Out of anger, Lera flared her nostrils, and already sterner, looked into her eyes.

- All right, Im silent.

### You will also be interested:

- Seal team 6
- Hire a scientist nms
- Marble background wallpaper
- 1800 sf home plans
- Harry potter wattpad
- Used volkswagen las vegas
- Exch mail umd edu
- Axis nvr 16 channel
- How to pronounce equipped
- Ufc 4 combos
- Sav on plating
- Sit foundation bass strings
- Hebrew name to english

You can come and meet. I looked across the table at the ogre woman. As for this kind of creatures, she was even quite attractive and it is clear that she was watching herself.

**3998**3999 4000