Learn R Programming

fifer (version 1.1)

subsetString: Extract only part of a string

Description

Extract only part of a string, given a separator

Usage

subsetString(string, sep = " ", position = 3, flexible = FALSE)

Arguments

string
a vector of strings
sep
the separator that separates the parts of the strings
position
the position of the element you wish to extract
flexible
Force the function to find the string "sep"? See details

Value

the element the user wishes to extract

Details

Given a string with a separator (e.g., "Subject 001", where the separator is a space), this function can be used to extract only whatever follows the separator (in this case, "001"). It is often used when data comes in with a conglomorated identifier (such as case-matchNumber-drawNumber-Month). The "flexible" function is for instances where a particular string (i.e., sep) is found in only some elements of string. For example, if the string is c("...call..ID", "...call..Gender", "ethnicity"), if flexible is false, it will search for the string "...call.." in the string "ethnicity", and, not finding it, will return NA. To overcome this, flexible tells the function to only perform the operation on those parts of the vector that contain the string sep.

Examples

Run this code
barcode = c("Case-001-B", "Control-001-A", "Case-002-A", "001")
subsetString(barcode, sep="-", position=2, flexible=TRUE)
subsetString(barcode, sep="-", position=3, flexible=TRUE)
subsetString(barcode, sep="-", position=3, flexible=FALSE)

Run the code above in your browser using DataLab