gx.subset: Extracts a Subset of Rows from a Data Frame

Description Usage Arguments Details Value Note Author(s) See Also Examples

Description

The function extracts a subset of rows, and columns if required, from a data frame and returns the subset as a new data frame based on the criterion provided by the user. Unused factor names are dropped.

Usage

1
gx.subset(dfname, subset = TRUE)

Arguments

dfname

name of the data frame from which rows are to be extracted.

subset

the criterion for selecting the subset (rows).

Details

The subset criterion can be ‘complex’ and be a combination of conditions, see Examples below.

Value

data

a data frame only containing the rows of the input data frame where the criterion is met.

Note

This function is based on a script shared by Bill Venables on S-News, October 10, 1997. As such it may pre-date the time that subset was added to the S-Plus library. It is simple to use and has been retained.

Author(s)

William N. Venables

See Also

subset

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
## Make test data available
data(kola.c)

## Make a subset of the data for Finland
finland.c <- gx.subset(kola.c, COUNTRY == "FIN")

## Make a subset of the data for rock type, LITHO 82 occurring
## in Russia. Note that both COUNTRY and LITHO are factor variables 
russia.82 <- gx.subset(kola.c, COUNTRY == "RUS" & LITHO == 82)

## Make a subset of the data for Cu exceeding 50(ppm) in Norway
norway.cugt50 <- gx.subset(kola.c, COUNTRY == "NOR" & Cu >50)

## Make single element subsets, e.g. for use with function gx.cnpplts
## First locate the column in the data frame where the element of
## interest is stored using dimnames(kola.c)[[2]], we find that Be is
## the 19th column in the data frame
dimnames(kola.c)[[2]]
Norway <- gx.subset(kola.c,COUNTRY=="NOR")[,19]
Russia <- gx.subset(kola.c,COUNTRY=="RUS")[,19]
Finland <- gx.subset(kola.c,COUNTRY=="FIN")[,19]

## Clean-up
rm(finland.c)
rm(russia.82)
rm(norway.cugt50)
rm(Norway)
rm(Russia)
rm(Finland)

rgr documentation built on May 2, 2019, 6:09 a.m.

Related to gx.subset in rgr...