What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Subsetting in R Programming | subset() function

  • Sep 09, 2023
  • 7 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Abhisek Ganguly
Subsetting in R Programming | subset() function

R is a programming language for statistical computing, to work with Data. Data manipulation is at the core of any data analysis or statistical modeling process. One of the fundamental operations in data manipulation is subsetting, which allows you to extract specific portions of your data that are relevant to your analysis. In the R programming language, subsetting is a powerful and versatile technique that can help you filter, explore, and transform your data efficiently. In this article, we will learn about subsetting in R, exploring its various methods and use cases.

What is Subsetting in R?

Subsetting in R refers to the process of selecting specific elements or subsets of a data structure, such as vectors, matrices, data frames, or lists. This operation is crucial when you want to work with a portion of your data rather than the entire dataset.

Subsetting allows you to:

  1. Focus on relevant data: Extract only the data you need for your analysis, making your code more efficient and your results more interpretable.
  2. Filter data: Remove unwanted observations or variables to simplify your analysis.
  3. Create subsets for different analyses: Split your data into multiple subsets for comparative or exploratory analysis.
  4. Perform conditional operations: Apply operations to specific subsets of your data based on certain conditions.

Let's explore the various ways to subset data in R:

Subsetting Vectors

In R, a vector is a basic data structure that can hold elements of the same data type, such as numbers or characters. Subsetting vectors are straightforward and can be achieved using square brackets `[ ]`.

Here's how you can subset a vector `x` to select specific elements:

x <- c(1, 2, 3, 4, 5)
subset <- x[c(2, 4)]

 

In this example, variable `subset` will contain the values 2 and 4.

You can also use logical conditions to subset a vector:

x <- c(1, 2, 3, 4, 5)
subset <- x[x > 2]

 

Now, the variable `subset` will contain `3`, `4`, and `5`, because of them being greater than 2.

Subsetting Matrices

Matrices are two-dimensional arrays with rows and columns. In R, subsetting matrices is similar to subsetting vectors but requires specifying both row and column indices.

To subset a matrix `mat`, you can use the `[row_indices, column_indices]` notation: 

mat <- matrix(1:9, nrow = 3)
subset <- mat[2, 3]

 

This code will select the element from the 2nd row and 3rd column. Hence, `subset` will contain the value `6`.

We can also subset matrices based on conditions:

mat <- matrix(1:9, nrow = 3)
subset <- mat[mat > 4]

 

Now, the variable `subset` will contain 5, 6, 7, 8, and 9, as they are all greater than 4.

Subsetting Lists

Lists in the data structure are an ordered data structure that stores elements sequentially and can be accessed by the index of the elements. Lists in R can contain elements of different types, including vectors, matrices, and other lists. 

To subset a list `my_list`, you can use double square brackets `[[ ]]`:

subset <- my_list[[2]]

 

This will create a subset with the index 2 element of the list.

Subsetting Data Frames

Data frames are one of the most common data structures in R, often used to store tabular data. Subsetting data frames is a crucial part of data manipulation and is widely used in data science techniques.

To subset a data frame `df`, you can use the `$` operator or square brackets `[ ]`: 

# Using the $ operator
subset1 <- df$column_name
# Using square brackets
subset2 <- df[, "column_name"]

 

In this code snippet, we are storing only a specified column in a subset using two methods.

We can apply conditional subsetting to data frames as well:

subset <- df[df$age > 30,]

 

In this example, the variable `subset` will contain all rows where the `age` column is greater than `30`.

The subset() Function in R

In addition to the raw data manipulation methods mentioned above, R also provides us with a built-in function called `subset()` that simplifies data frame subsetting. The `subset()` function is particularly useful when working with data frames, as it allows you to express subsetting conditions more concisely.

The syntax of the subset() function is as follows:

subset(x, subset, select, drop = FALSE, …)

 

Here:

  • x: Object to be subsetted. Could be any of the Vector, data.frame, & matrices.
  • subset: Subset expression.
  • select: Columns to select in a vector.
  • drop: Passed on to the indexing method for matrices and data frames.
  • '...': Other customizable arguments.

 Let us consider a data frame to properly understand the working of the `subset()` function.

  id name gender age state
r1 10 Steve M 23 NY
r2 11 Karl M 25 CA
r3 12 Chris M 24 NY
r4 13 Adam M 19 LA
r5 14 Ela F 20 NV
r6 15 Stephan M 22 CA
r7 16 Emma F 21 NY
r8 17 Sophie F 23 LA

 

Subset by Row and Column names

We can use the `subset()` function and subset the data frame by rows and column names.

subset(df,gender=='M',select=c('id','name','state'))

 

Here we are subsetting the data frame by `id`, `name`, and `state`. We are subsetting the data frame by column in this example.

Let us now apply the `subset()` function to the rows.

subset(df, subset=rownames(df) == 'r1')

 

Here we are explicitly printing out the first row of the data frame, using the function `rownames()` inside of `subset()`. This will create a subset with only the first row of the column.

If we want multiple rows we can list them out as follows:

subset(df, rownames(df) %in% c('r1','r2','r3'))

 

In this example above, we are selecting the rows 1, 2, and 3 respectively, and storing them into a subset.

Subset by Conditions

 We can subset a data frame based on conditions as well. Here's a small example of how we can do it. We are subsetting our data frame based on age greater than 20, and printing out the name and gender of those who qualify.

subset(df, subset = Age > 20, select = c('name', 'gender'))

 

We can also stack multiple conditions to build the subset: 

subset(df, gender=='M' | state == 'PH')
subset(df, gender=='M' & state %in% c('PH'))

 

Both of these lines of code do the same thing, only the writing style differs. 

I'd suggest that you replicate these onto your personal computer and work with it, play around a little, and figure out the workings. You can easily create the data frame using the following code:

df <- data.frame(
  id = c(10, 11, 12, 13, 14, 15, 16, 17),
  name = c("Steve", "Karl", "Chris", "Adam", "Ela", "Stephan", "Emma", "Sophie"),
  gender = c("M", "M", "M", "M", "F", "M", "F", "F"),
  age = c(23, 25, 24, 19, 20, 22, 21, 23),
  state = c("NY", "CA", "NY", "LA", "NV", "CA", "NY", "LA")
)

 

Conclusion

Subsetting is a fundamental skill to learn in R, enabling you to extract, manipulate, and analyze data efficiently. Whether you are working with vectors, matrices, data frames, or lists, mastering subsetting techniques is essential for becoming a proficient Data Scientist or making good data-driven decisions.

Now that you have a solid foundation in subsetting, it's time to put this knowledge to use in your data analysis projects. Happy coding!

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Abhisek Ganguly
Passionate machine learning enthusiast with a deep love for computer science, dedicated to pushing the boundaries of AI through academic research and sharing knowledge through teaching.