replace_to: Grab Begin/End of String to/from Character

Description Usage Arguments Value Author(s) References Examples

View source: R/replace_to.R

Description

replace_to - Grab from beginning of string to a character(s).

replace_from - Grab from character(s) to end of string.

Usage

1
2
3
replace_to(x, char = " ", n = 1, include = FALSE, ...)

replace_from(x, char = " ", n = 1, include = FALSE, ...)

Arguments

x

A character string

char

The character from which to grab until/from.

n

Number of times the character appears before the grab.

include

logical. If TRUE includes the character in the grab.

...

ignored.

Value

returns a vector of text with begin/end of string to/from character removed.

Author(s)

Josh O'Brien and Tyler Rinker <tyler.rinker@gmail.com>.

References

http://stackoverflow.com/q/15909626/1000343

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## Not run: 
x <- c("a_b_c_d", "1_2_3_4", "<_?_._:")
replace_to(x, "_")
replace_to(x, "_", 2)
replace_to(x, "_", 3)
replace_to(x, "_", 4)
replace_to(x, "_", 3, include=TRUE)

replace_from(x, "_")
replace_from(x, "_", 2)
replace_from(x, "_", 3)
replace_from(x, "_", 4)
replace_from(x, "_", 3, include=TRUE)

x2 <- gsub("_", " ", x)
replace_from(x2, " ", 2)
replace_to(x2, " ", 2)

x3 <- gsub("_", "\\^", x)
replace_from(x3, "^", 2)
replace_to(x3, "^", 2)

x4 <- c("_a_b", "a__b")
replace_from(x4, "_", 1)
replace_to(x4, "_", 1)

## End(Not run)

Example output

[1] "a" "1" "<"
[1] "a_b" "1_2" "<_?"
[1] "a_b_c" "1_2_3" "<_?_."
[1] "a_b_c_d" "1_2_3_4" "<_?_._:"
[1] "a_b_c_" "1_2_3_" "<_?_._"
[1] "b_c_d" "2_3_4" "?_._:"
[1] "c_d" "3_4" "._:"
[1] "d" "4" ":"
[1] "a_b_c_d" "1_2_3_4" "<_?_._:"
[1] "_d" "_4" "_:"
[1] "c d" "3 4" ". :"
[1] "a b" "1 2" "< ?"
[1] "c^d" "3^4" ".^:"
[1] "a^b" "1^2" "<^?"
[1] "a_b" "_b" 
[1] ""  "a"

textclean documentation built on May 2, 2019, 7:22 a.m.