clean_dvec: Clean a vector of delimited strings

View source: R/clean_dvec.R

clean_dvecR Documentation

Clean a vector of delimited strings

Description

Clean a vector of delimited strings

Usage

clean_dvec(vec, sep = ";")

Arguments

vec

A vector of delimited strings to clean

sep

String - delimiter used within each element of the vector to be cleaned

Details

This function handles cases where delimited data is stored within each element of a vector. Such vectors are in a sense vectors of vectors, though each element is stored as a delimited string. In addition, these vectors resemble lists, which can store a sequence of vectors. The function will loop through each element of the input vector, check for the presence of a string containing the specified delimiter, split the element string apart by the delimiter, attempt to convert to numeric, and then sort the unique values contained within the split string.

Value

Vector - the cleaned input vector, with each element consisting of sorted, unique elements

Examples

df <- data.frame("mycol" = c("B; A; M", "D; O; G", "6; 2; 9; 10; 12"), 
                 stringsAsFactors = FALSE)
head(df$mycol)
df$mycol <- clean_dvec(df$mycol, sep = "; ")  
head(df$mycol)               

etnite/bwardr documentation built on Jan. 6, 2023, 7:12 a.m.