R language String substitution

Source: Internet
Author: User
Tags control characters locale php tutorial printable characters alphanumeric characters stringr

R gsub Function

gsub()function replaces all matches of a string, if the parameter is a string vector, returns a string vector of the same Length and with the same attributes (after possible coercion to character). Elements of string vectors which is not substituted would be returned unchanged (including any declared encoding).

Gsub (pattern, replacement, X, Ignore.case = False, Perl = False,    fixed = false, Usebytes = False)


pattern : String to is matched
replacement : String for replacement
x : string or string vector
ignore.case : If TRUE, ignore case
...

> x <-"R Tutorial" > Gsub ("ut", "OT", x)
[1] "R totorial"


Case insensitive Replace:

> gsub ("Tut", "ot", x,ignore.case=t))
[1] "R otorial"


If Ignore.case is isn't set to True, no replace take place:

> gsub ("Tut", "OT", x)
[1] "R Tutorial"

> x <-"line 4322:he are now years old, and weights 130lbs" > y <-gsub ("\\d+", "---", x) > Y
[1] "Line---: He's now---years-old, and weights---lbs"

> x <-"line 4322:he are now years old, and weights 130lbs" > y <-gsub ("[[: Lower:]]", "-", x) > Y
[1] "----4322:h--------------,-------------"


Vector Replacement:

> x <-C ("R Tutorial", "PHP Tutorial", "HTML Tutorial") > Gsub ("Tutorial", "Examples", X)
[1] "R Examples" "    PHP Examples" "  HTML Examples"




Regular Expression Syntax:
Syntax Description
\\d Digit, 0,1,2 ... 9
\\d Not Digit
\\s Space
\\s Not Space
\\w Word
\\w Not Word
\\t Tab
\\n New Line
^ Beginning of the string
$ End of the string
\ Escape special characters, e.g. \ \ ", \+ is" + "
| Alternation match. e.g./(e|d) n/matches "en" and "DN"
Any character, except \ or line terminator
[AB] A or B
[^ab] Any character except A and b
[0-9] All Digit
[A-z] All uppercase A to Z letters
[A-z] All lowercase A to Z letters
[A-z] All uppercase and lowercase A to Z letters
i+ I at least one time
i* I zero or more times
I? I zero or 1 time
I{n} I occurs n times in sequence
I{N1,N2} I occurs n1-n2 times in sequence
I{N1,N2}? Non greedy match, see above example
I{n,} I occures >= n times
[: Alnum:] Alphanumeric characters: [: Alpha:] and [:d Igit:]
[: Alpha:] Alphabetic characters: [: Lower:] and [: Upper:]
[: Blank:] Blank characters:e.g. Space, Tab
[: Cntrl:] Control characters
[:d Igit:] digits:0 1 2 3 4 5 6 7 8 9
[: Graph:] Graphical characters: [: Alnum:] and [:p UNCT:]
[: Lower:] lower-case letters in the current locale
[:p rint:] Printable characters: [: Alnum:], [:p UNCT:] and Space
[:p UNCT:] Punctuation character:! "# $% & ' () * +,-. / : ; < = >? @ [ \ ] ^ _ ` { | } ~
[: Space:] Space Characters:tab, newline, vertical tab, form feed, carriage return, space
[: Upper:] Upper-case letters in the current locale
[: Xdigit:] Hexadecimal digits:0 1 2 3 4 5 6 7 8 9 a B c D E f a b c D E F

REF:

http://www.endmemo.com/program/R/gsub.php

Http://cran.r-project.org/web/packages/stringr/stringr.pdf

Http://stackoverflow.com/questions/11936339/in-r-how-do-i-replace-text-within-a-string

R language String substitution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.