R language-string processing functions, string functions

Source: Internet
Author: User

R language-string processing functions, string functions
Nchar

Function for retrieving the number of characters
Different from nchar, length is the length of the orientation.

# Nchar indicates the number of characters in the string. nchar ("abcd") [1] 4 # length indicates the number of elements in the vector. length ("abcd ") [1] 1 length (c ("hello", "world") [1] 2
Paste and paste0

String Bonding Function
When no Delimiter is specified for paste, the default Delimiter is a space.
Paste0 is blank by default when no Delimiter is specified

# Paste ("Hello", "world") [1] "Hello world" # No space paste0 ("Hello", "world") is separated by spaces by default ") [1] "Helloworld" # specify the delimiter paste ("abc", "efg", "hijk", sep = "-") [1] "abc-efg-hijk" # connect each element of the vector to paste0 ("A", sep = "") [1] "A1" "A2" A3 "" A4 "" A5 "" A6 "# collapse parameter: after each element operation, connect every element of the vector to paste0 ("A", sep = "", collapse = "-") [1] "A1-A2-A3-A4-A5-A6"
Substr

String truncation Function

substr(x = "hello", start = 1, stop = 2)[1] "he"
Strsplit

String delimiter to generate a list

strsplit("abc", split = "")[[1]][1] "a" "b" "c"

If you want to use this function for a vector, pay attention to it.

# Divide each element of a vector and obtain the unlist (lapply (X = c ("abc", "bcd", "dfafadf") of the first element after the split "), FUN = function (x) {return (strsplit (x, split = "") [[1] [1])}) [1] "a" "B" "d"
Gsub and sub

String replacement
Gsub replaces all matched
Replace the first matched sub

# Replace B with Bgsub (pattern = "B", replacement = "B", x = "baby") [1] "BaBy" gsub (pattern = "B ", replacement = "B", x = c ("abcb", "boy", "baby ")) [1] "aBcB" "Boy" "BaBy" # Replace only the first bsub (pattern = "B", replacement = "B", x = "baby ") [1] "Baby" sub (pattern = "B", replacement = "B", x = c ("abcb", "baby ")) [1] "aBcb" "Baby"
Grep and grepl

String Matching
The grep function returns the index value.
The grepl function returns the logical value.

# Returns the index grep (pattern = "boy", x = c ("abcb", "boy", "baby") of the matched element ")) [1] 2 # Return the logical value grepl (pattern = "boy", x = c ("abcb", "boy", "baby") [1] FALSE TRUE FALSE
Match & pmatch & charmatch

1. match
Usage
Match (x, table, nomatch = NA_integer _, incomparables = NULL)
X % in % table

Parameters:
X: vector or NULL: the values to be matched. Long vectors are supported.

Table: vector or NULL: the values to be matched against. Long vectors are not supported. (matched value)

Nomatch: the value to be returned in the case when no match is found. Note that it is coerced to integer. (no value returned on match)

Incomparables: a vector of values that cannot be matched. any value in x matching a value in this vector is assigned the nomatch value. for historical reasons, FALSE is equivalent to NULL. (different matching values)

The match function is similar to % in %. The difference is that match returns an index, while % in % returns a logical value.

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.