R软件中的subset()函数
(2012-03-15 10:48:22)
标签:
杂谈 |
分类: R软件学习 |
subset {base} | R Documentation |
Subsetting Vectors, Matrices and Data Frames
Description
Return subsets of vectors, matrices or data frames which meet conditions.
Usage
subset(x, ...) ## Default S3 method: subset(x, subset, ...) ## S3 method for class 'matrix' subset(x, subset, select, drop = FALSE, ...) ## S3 method for class 'data.frame' subset(x, subset, select, drop = FALSE, ...)
Arguments
x |
object to be subsetted. |
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 [ |
... |
further arguments to be passed to or from other methods. |
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 x[subset &
!is.na(subset)]
.
For data frames,
the subset
subset
The select
The drop
Factors may have empty levels after subsetting; unused levels
are not automatically removed. See droplevels
Value
An object similar
to x
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 subset
Author(s)
Peter Dalgaard and Brian Ripley
See Also
Examples
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)