R语言 字符串判断

Python016

R语言 字符串判断,第1张

# R语言判断字符是否包含某字符,需要导入stringr包,两个函数都是可以的

stringr::str_detect(table$cust_id,'123')

grepl('123',table$cust_id)

stringr 包中的大部分函数具有统一风格的命名方式,以 str_ 开头,正则表达式也完全适用该包。

字符串拼接函数 str_c ,与R语言自带的 paste 和 paste0 函数具有相同的作用。

字符计数函数 str_count ,计算字符串中指定字符的个数。

字符检查函数 str_detect ,检查字符串中是否包含指定字符,返回逻辑向量。

字符复制函数 str_dup ,将字符向量重复若干次,返回重复后的字符向量。

字符提取函数 str_extract 和 str_extract_all ,对字符串进行提取, str_extract_all 函数返回所有的匹配结果。

字符串格式化函数 str_glue ,用花括号 {} 表示占位符,括号内的变量被替换成全局变量值。

字符串长度函数 str_length ,计算字符串长度。

字符位置提取函数 str_locate 和 str_locate_all ,返回匹配到的字符的位置。

字符匹配函数 str_match 和 str_match_all 与字符提取函数 str_extract 类似,返回匹配到的字符,不同之处在于返回格式。

字符补齐函数 str_pad ,用于在字符串中添加单个字符,可选择添加的位置,在参数 side 中进行设置。

字符删除函数 str_remove 和 str_remove_all ,用于删除字符串中的部分字符。

字符替换函数 str_replace 、 str_replace_all 和 str_replace_na ,用于替换字符串中的部分字符。

字符排序函数 str_sort 和 str_order ,对字符向量进行排序。

字符分割函数 str_split 和 str_split_fixed ,对字符串进行分割。

字符过滤函数 str_sub 和 str_subset , str_sub 函数通过指定开始和结束位置,过滤出字符串的部分字符串。 str_subset 函数通过匹配模式,过滤出满足模式的字符串。

stringr 包中其他的有用函数,用于常见的字符处理。

R语言之—字符串处理函数

nchar

取字符数量的函数

length与nchar不同,length是取向量的长度

# nchar表示字符串中的字符的个数

nchar("abcd")

[1] 4

# length表示向量中元素的个数

length("abcd")

[1] 1

length(c("hello", "world"))

[1] 2

chartr

字符替换

chartr(old="a", new="c", x="a123")

[1] "c123"

chartr(old="a", new="A", x="data")

[1] "dAtA"

paste和paste0

字符串粘合函数

paste在不指定分割符的情况下,默认分割符是空格

paste0在不指定分割符的情况下,默认分割符是空

# 默认以空格隔开

paste("Hello","world")

[1] "Hello world"

# 没有空格

paste0("Hello","world")

[1] "Helloworld"

# 指定分割符

paste("abc", "efg", "hijk", sep = "-")

[1] "abc-efg-hijk"

# 分别对向量的每一个元素进行连接

paste0("A", 1:6, sep = "")

[1] "A1" "A2" "A3" "A4" "A5" "A6"

# collapse参数:每一个元素操作之后,再把向量的每一个元素进行连接

paste0("A", 1:6, sep = "",collapse = "-")

[1] "A1-A2-A3-A4-A5-A6"

substr

字符串截取函数

substr(x = "hello", start = 1, stop = 2)

[1] "he"

strsplit

字符串的分割函数,可以指定分割符,生成一个list

strsplit("abc", split = "")

[[1]]

[1] "a" "b" "c"

如果要对一个向量使用该函数,需要注意。

# 分割向量的每一个元素,并取分割后的第一个元素

unlist(lapply(X = c("abc", "bcd", "dfafadf"), FUN = function(x) {return(strsplit(x, split = "")[[1]][1])}))

[1] "a" "b" "d"

gsub和sub

字符串替换

gsub替换匹配到的全部

sub 替换匹配到的第一个

# 将b替换为B

gsub(pattern = "b", replacement = "B", x = "baby")

[1] "BaBy"

gsub(pattern = "b", replacement = "B", x = c("abcb", "boy", "baby"))

[1] "aBcB" "Boy" "BaBy"

# 只替换第一个b

sub(pattern = "b", replacement = "B", x = "baby")

[1] "Baby"

sub(pattern = "b", replacement = "B", x = c("abcb", "baby"))

[1] "aBcb" "Baby"

grep和grepl

字符串匹配

grep函数返回的是索引值

grepl函数返回的是逻辑值

# 返回匹配到的元素的索引

grep(pattern = "boy", x = c("abcb", "boy", "baby"))

[1] 2

# 返回逻辑值

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

参数:

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. (被匹配的值)

nomatch: the value to be returned in the case when no match is found. Note that it is coerced to integer. (没有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. (不同来匹配的值)

match函数类似与 %in%,不同的是match返回的是索引,而%in%返回的是逻辑值。