str_split: Split a Vector of Strings Following a Regular Structure

Description Usage Arguments Details See Also Examples

View source: R/str_split.R

Description

This function takes a vector of strings following a regular structure, and converts that vector into a data.frame, split on that delimiter. A nice wrapper to strsplit, essentially - the primary bonus is the automatic coersion to a data.frame.

Usage

1
2
3
4
5
str_split(x, sep, fixed = FALSE, perl = TRUE, useBytes = FALSE,
  names = NULL)

split2df(x, sep, fixed = FALSE, perl = TRUE, useBytes = FALSE,
  names = NULL)

Arguments

x

a vector of strings.

sep

the delimiter / regex you wish to split your strings on.

fixed

logical. If TRUE, we match sep exactly; otherwise, we use regular expressions. Has priority over perl.

perl

logical. Should perl-compatible regexps be used? Ignored when fixed is TRUE.

useBytes

logical. If TRUE, matching is done byte-by-byte rather than character-by-character.

names

optional: a vector of names to pass to the returned data.frame.

Details

Note that the preferred method for reading text data with a single, one character delimiter is through read.table(text=...) or data.table::fread; however, this function is helpful in the case of non-regular delimiters (that you wish to specify with a regex)

See Also

strsplit

Examples

1
2
3
4
5
6
7
str_split(
  c("regular_structure", "in_my", "data_here"),
  sep="_",
  names=c("apple", "banana")
)
x <- c("somewhat_different.structure", "in_this.guy")
str_split( x, "[_\\.]", names=c("first", "second", "third") )

Example output

    apple    banana
1 regular structure
2      in        my
3    data      here
     first    second     third
1 somewhat different structure
2       in      this       guy

Kmisc documentation built on May 29, 2017, 1:43 p.m.

Related to str_split in Kmisc...