# 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, …)
xcontain just the selected elements (for a vector), rows and columns (for a matrix or data frame), and so on.
[, and in particular the non-standard evaluation of argument
subsetcan have unanticipated consequences.
x[subset & !is.na(subset)]. For data frames, the
subsetargument works on the rows. Note that
subsetwill be evaluated in the data frame, so columns can be referred to (by name) as variables in the expression (see the examples). The
selectargument 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
dropargument 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
droplevelsfor a way to drop all unused levels from a data frame.
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)