min and pmin: Minimum and Parallel Minimum Description Usage

advertisement
Back to: R plain language help files
Author: Kristine Hunter
min and pmin: Minimum and Parallel Minimum
Description
min
Returns the smallest value present in a string of numbers or characters (vector).
pmin
Takes more than one vector and produces a single vector showing the smallest value
for each set of data points, the parallel minimum.
Usage
min(x, na.rm=)
pmin(x,y,z, na.rm=)
Arguments
x,y,z
numeric or character vectors (must be of equal length for pmin).
na.rm=
used to indicate whether missing values should be removed.
na.rm=FALSE: NA values in the input will produce NA value in the output.
na.rm=TRUE: NA values in the input will be ignored.
Details
You must supply a column or row of values from a data frame
( min(dataframe$variable name) ) or input values directly (see Examples section).
The inputs can be numeric or character arguments. Character arguments will be sorted
lexicographically based on the collating sequence of the locale in use. There are many different
locales and therefore many different ways of sorting; for ease of use it may be prudent to give
character arguments numeric values that work for your dataset. (For information on the
collating sequence of locales see Comparison help page (enter help(Comparison) into
R).
Value
The min function returns the smallest value in the specified column or row (Example 1). If
more than one column or row is specified, it returns the smallest value present in any of the
columns or rows (Example 2).
The pmin function takes one or more vectors as arguments and returns a single vector giving
the parallel minima of the vectors. The first term produced is the smallest value present in the
first term of all the arguments, the second term produced is the smallest value present in the
second term of all the arguments and so on. Each of the vectors must be of the same length, and
the output will also be the same length (Example 3).
If any of the values in the specified column or row are NA then NA will be returned if
na.rm=FALSE (Example 4). If na.rm=TRUE then NA values will be ignored (Example 5). If
you do not specify na.rm, R will default to FALSE.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth &
Brooks/Cole.
Crawley, M. J. (2007) The R Book. John Wiley & Sons Ltd.
R-help page Comparison {stats}
http://stat.ethz.ch/R-manual/R-devel/library/base/html/Comparison.html
R-help page min {stats} http://stat.ethz.ch/R-manual/R-devel/library/base/html/Extremes.html
See Also
(enter help(range)etc. directly into R)
range
provides both the largest and smallest values present in a vector
which.min
the column or row number where the smallest value is located (if the smallest
value is repeated only the first location will be returned)
pmin.int
faster version of pmin, only works with atomic vectors with no classes
Examples
(the entire contents below can be copied and pasted directly into R)
##Example 1
test1<-c(1,2,3,5,8,13)
min(test1)
##Example 2
test1<-c(1,2,3,5,8,13)
test2<-c(72,36,18,9,4,2)
min(test1,test2)
##Example 3
test1<-c(1,2,3,5,8,13)
test2<-c(72,36,18,9,4,2)
pmin(test1,test2)
##Example 4
test1<-c(1,2,3,5,8,NA)
min(test1,na.rm=FALSE)
##Example 5
test1<-c(1,2,3,5,8,NA)
min(test1,na.rm=TRUE)
Download