Extract feature names by splitting a given column by a separator and keeping the Nth entry.

getFirstId(df, colName, separator = ";")

getNthId(df, colName, N, separator = ";")

Arguments

df

A data.frame.

colName

Character scalar indicating which of the columns in df to consider.

separator

Character scalar giving the separator to split the column entries by.

N

Numeric scalar indicating which part of the column elements to extract, after separating by separator.

Value

A vector with extracted feature identifiers.

Author

Charlotte Soneson

Examples

df <- data.frame(x = c("g1;p1;h2", "g2;p1;h3"))
getFirstId(df, colName = "x", separator = ";")
#> [1] "g1" "g2"
getNthId(df, colName = "x", N = 2, separator = ";")
#> [1] "p1" "p1"

## Return value will be NA if the field doesn't exist
df <- data.frame(x = c("g1;p1;h2", "g2"))
getFirstId(df, colName = "x", separator = ";")
#> [1] "g1" "g2"
getNthId(df, colName = "x", N = 2, separator = ";")
#> [1] "p1" NA