Subsetting R Objects

Part of the R For Dummies Cheat Sheet (UK Edition)

Vectors, lists, and data frames play an important role in representing data in R, so being able to succinctly and correctly specify a subset of your data is important.

You can use three operators to subset your data:

  • $: Extracts a single element by name from a list or data frame. For example, iris$Sepal.Length extracts the column Sepal.Length from the data frame iris.

  • [[: Extracts a single element by name or position from a list or data frame. For example, iris[["Sepal.Length"]] extracts the column Sepal.Length from the data frame iris; iris[[2]] extracts the second element from iris.

  • [: Extracts multiple elements from a vector, array, list, or data frame. For example, iris[, c("Sepal.Length", "Species")] extracts the columns Sepal.Length and Species from iris; iris[1:10, ] extracts the first ten rows from iris; and iris[1:10, "Species"] extracts the first ten elements of the column Species from iris.

The bracket subset operator, [, allows you to return multiple elements. You specify the subset in one of five ways:

  • Blank: Returns everything. For example, iris[] returns all of iris.

  • Positive numeral: Includes only these elements. For example, iris[1:100, 5] extracts the first hundred elements of the fifth column of iris.

  • Negative numeral: Excludes these elements. For example, iris[-(1:100), ] excludes the first hundred rows from iris, while iris[, -5] excludes the fifth column from iris.

  • Logical: Includes if TRUE; excludes if FALSE. For example, iris[iris$Species=="setosa", ] extracts only those rows from iris where the Species value is "setosa".

  • Name: Includes all names that match. For example, iris[, c("Species", "Petal.Width")] extracts the columns Species and Petal.Width from iris.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus

SERIES
R For Dummies Cheat Sheet (UK Edition)

Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.