Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common string processing methods in R language

Shulou Source: shulou.com Published: 2022-06-01 07:26:07 10月03日 Update

This article introduces the knowledge of "what are the common string processing methods in R language". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Basic string processing common in R language, involving matching, substitution, segmentation, interception, case conversion, etc.

A = c ("abcdgegh") B = c ("abcdgegh", "deghgabcd")

Match

Grep (pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE, fixed = FALSE, useBytes = FALSE, invert = FALSE) grepl (pattern, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE) # # ignore.case determines whether the match is case-sensitive. In order to achieve accurate matching, the default is case-sensitive; you can set it to be insensitive

Grep () returns a matching result, where the invert setting returns a string on a match or an unmatched string, and grepl () returns a logical value, for example:

> grep ("ab", A) [1] 1 > grep ("ab", Bmaim valueholders) [1] "abcdgegh"deghgabcd" > grep ("de", Bmaejinghuo valueful) [1] "deghgabcd" > grep ("de", B Meijing valueholders) [1] "abcdgegh" # grepl string starting match can also be anchored with "^" > grepl ("ab", A) [1] TRUE > grepl ("ab", B) [1] TRUE TRUE

Match and replace

Sub (pattern, replacement, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE) gsub (pattern, replacement, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE) chartr (old, new, x)

Sub (), gsub (), chartr () can return the result after replacement

> sub ("ab", "AB", B) [1] "ABcdgegh"deghgABcd" > gsub ("ab", "AB", B) [1] "ABcdgegh"deghgABcd" > chartr ("a", "A", B) [1] "Abcdgegh"deghgAbcd"

Split, split, intercept

Strsplit (x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE) substr (x, start, stop) substring (text, first, last = 1000000L)

For example:

> strsplit (A, "d") [[1]] [1] "abc", "gegh" > unlist (strsplit (A, "d")) [1] "abc", "gegh" > substr (A deg 1) [1] "abc" > substr (BMI 1 deg 3) [1] "abc", "deg" > substring (A Last 4) [1] "abcd" > substring (BJI Lasters 4) [1] "abcd"degh" >

Case replacement

# # replace with uppercase toupper (x) # # replace with lowercase tolower (x) # # convert uppercase and lowercase casefold according to parameters (xmeme uppercase false)

For example:

> toupper (A) [1] "ABCDGEGH" > tolower (toupper (A)) [1] "abcdgegh" > casefold (A heroin uppermost T) [1] "ABCDGEGH"

In addition, there is a character-specific package strngr that can perform a variety of operations on strings.

Package: stringr

This is the end of the content of "what are the common string processing methods in R language?" Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Characters strings processing common language size lowercase methods content to big more knowledge results practical accurate learned and then parameters dilemmas multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia vpn Docker MySQL macOS