Learn R Programming

rgr (version 1.1.0)

gx.subset: Extracts a Subset of Rows from a Dataframe

Description

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

Usage

gx.subset(dfname, subset = TRUE)

Arguments

dfname
name of the dataframe from which rows are to be extracted.
subset
the criterion for selecting the subset (rows).

Value

  • dataa dataframe only containing the rows of the input dataframe where the criterion is met.

Details

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

See Also

subset

Examples

Run this code
## 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 dataframe where the element of
## interest is stored using dimnames(kola.c), we find that Be is the
## 19th column in the dataframe
dimnames(kola.c)
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(Norway)
rm(Russia)
rm(Finland)

Run the code above in your browser using DataLab