Jeason
2440字约8分钟
2019-08-18
str_length: 字符串长度 str_count: 字符串计数 str_order: 字符串排序
str_length(string)
> str_length(c("I", "am", NA))
[1] 1 2 2 NA
str_count(string, pattern = "")
对字符串中匹配的字符计数
> str_count('aaa444sssddd', "a")
[1] 3
对字符串向量中匹配的字符计数
> fruit <- c("apple", "banana", "pear", "pineapple")
> str_count(fruit, "a")
[1] 1 3 1 1
对于一些特殊字符如:".",需要进行正则匹配
str_order(x, decreasing = FALSE, na_last = TRUE, locale = "en", numeric = FALSE, ...)
str_sort(x, decreasing = FALSE, na_last = TRUE, locale = "en", numeric = FALSE, ...)
> str_sort(letters[1:5])
[1] "a" "b" "c" "d" "e"
> str_order(letters[1:5])
[1] 1 2 3 4 5
## 按照数字进行排序
> x <- c("100a10", "100a5", "2b", "2a")
> str_sort(x)
[1] "100a10" "100a5" "2a" "2b"
> str_sort(x, numeric = TRUE)
[1] "2a" "2b" "100a5" "100a10"
str_c: 字符串拼接 str_trim: 去掉字符串的空格和TAB(\t) str_pad: 补充字符串的长度 str_dup: 复制字符串 str_wrap: 控制字符串输出格式 str_sub: 截取字符串
str_c(..., sep = "", collapse = NULL)
把多个字符串拼接为一个大的字符串。
> str_c('a','b')
[1] "ab"
> str_c('a','b',sep='-')
[1] "a-b"
> str_c(c('a','a1'),c('b','b1'),sep='-')
[1] "a-b" "a1-b1"
把多个向量参数拼接为一个大的字符串。
> str_c(head(letters), collapse = "")
[1] "abcdef"
# collapse参数,对多个字符串无效
> str_c('a','b',collapse = "-")
[1] "ab"
> str_c(c('a','a1'),c('b','b1'),collapse='-')
[1] "ab-a1b1"
拼接有NA值的字符串向量时,NA还是NA
> str_c(c("a", NA, "b"), "-d")
[1] "a-d" NA "b-d"
# Use str_replace_NA to display literal NAs:
> str_c(str_replace_na(c("a", NA, "b")), "-d")
[1] "a-d" "NA-d" "b-d"
str_flatten(string, collapse = "")
> str_flatten(letters)
[1] "abcdefghijklmnopqrstuvwxyz"
> str_flatten(letters, "-")
[1] "a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z"
str_trim() 从字符串的开头和结尾删除空格; str_squish() 同时还可以删除字符串内部的空格
str_trim(string, side = c("both", "left", "right"))
str_squish(string)
> str_trim("\n\nString with trailing and leading white space\n\n")
[1] "String with trailing, middle, and leading white space"
> str_squish("\n\nString with excess, trailing and leading white space\n\n")
[1] "String with trailing, middle, and leading white space"
str_pad(string, width, side = c("left", "right", "both"), pad = " ")
# 从左边补充空格,直到字符串长度为20
> str_pad("conan", 20, "left")
[1] " conan"
# 从右边补充空格,直到字符串长度为20
> str_pad("conan", 20, "right")
[1] "conan "
# 从左右两边各补充空格,直到字符串长度为20
> str_pad("conan", 20, "both")
[1] " conan "
# 从左右两边各补充x字符,直到字符串长度为20
> str_pad("conan", 20, "both",'x')
[1] "xxxxxxxconanxxxxxxxx"
str_dup(string, times)
> fruit <- c("apple", "pear", "banana")
> str_dup(fruit, 2)
[1] "appleapple" "pearpear" "bananabanana"
str_wrap(string, width = 80, indent = 0, exdent = 0)
str_sub(string, start = 1L, end = -1L)
str_sub(string, start = 1L, end = -1L, omit_na = FALSE) <- value
> hw <- "Hadley Wickham"
> str_sub(hw, 1, 6)
[1] "Hadley"
> str_sub(hw, end = 6)
[1] "Hadley"
> str_sub(hw, c(1, 8), c(6, 14))
[1] "Hadley" "Wickham"
> str_sub(hw, -1)
[1] "m"
> str_sub(hw, -7)
[1] "Wickham"
> str_sub(hw, end = -7)
[1] "Hadley W"
str_split: 字符串分割 str_subset: 返回匹配的字符串 word: 从文本中提取单词 str_detect: 检查匹配字符串的字符 str_match: 从字符串中提取匹配组 str_replace: 字符串替换 str_replace_na:把NA替换为NA字符串 str_locate: 找到匹配的字符串的位置 str_extract: 从字符串中提取匹配字符
str_split(string, pattern, n = Inf, simplify = FALSE)
str_split_fixed(string, pattern, n)
> fruits <- c("apples and oranges and pears and bananas", "pineapples and mangos and guavas")
> str_split(fruits, " and ")
[[1]]
[1] "apples" "oranges" "pears" "bananas"
[[2]]
[1] "pineapples" "mangos" "guavas"
> str_split(fruits, " and ", simplify = TRUE)
[,1] [,2] [,3] [,4]
[1,] "apples" "oranges" "pears" "bananas"
[2,] "pineapples" "mangos" "guavas" ""
> str_split(fruits, " and ", n = 3)
[[1]]
[1] "apples" "oranges" "pears and bananas"
[[2]]
[1] "pineapples" "mangos" "guavas"
> str_split_fixed(fruits, " and ", 3)
[,1] [,2] [,3]
[1,] "apples" "oranges" "pears and bananas"
[2,] "pineapples" "mangos" "guavas"
> str_split_fixed(fruits, " and ", 4)
[,1] [,2] [,3] [,4]
[1,] "apples" "oranges" "pears" "bananas"
[2,] "pineapples" "mangos" "guavas" ""
str_subset(string, pattern, negate = FALSE)
str_which(string, pattern, negate = FALSE)
> fruit <- c("apple", "banana", "pear", "pinapple")
> str_subset(fruit, "a")
[1] "apple" "banana" "pear" "pinapple"
> str_which(fruit, "a")
[1] 1 2 3 4
# 支持正则表达式
> str_subset(fruit, "^a")
[1] "apple"
> str_subset(fruit, "a$")
[1] "banana"
word(string, start = 1L, end = start, sep = fixed(" "))
> sentences <- c("Jane saw a cat", "Jane sat down")
> word(sentences, 1)
[1] "Jane" "Jane"
> word(sentences, 2)
[1] "saw" "sat"
> word(sentences, -1)
[1] "cat" "down"
> word(sentences, 2, -1)
[1] "saw a cat" "sat down"
# 匹配以..分割开的word
> str <- 'abc.def..123.4568.999'
> word(str, 1, sep = fixed('..'))
[1] "abc.def"
str_detect(string, pattern, negate = FALSE)
> fruit <- c("apple", "banana", "pear", "pinapple")
> str_detect(fruit, "a")
[1] TRUE TRUE TRUE TRUE
> str_detect(fruit, "^a")
[1] TRUE FALSE FALSE FALSE
> str_detect(fruit, "^p", negate = TRUE)
[1] TRUE TRUE FALSE FALSE
str_match(string, pattern)
str_match_all(string, pattern)
> val <- c("abc", 123, "cba")
> str_match(val, "a")
[,1]
[1,] "a"
[2,] NA
[3,] "a"
> str_match(val, "[0-9]")
[,1]
[1,] NA
[2,] "1"
[3,] NA
> str_match_all(val, "a")
[[1]]
[,1]
[1,] "a"
[[2]]
[,1]
[[3]]
[,1]
[1,] "a"
str_replace(string, pattern, replacement)
str_replace_all(string, pattern, replacement)
> fruits <- c("one apple", "two pears", "three bananas")
> str_replace(fruits, "[aeiou]", "-")
[1] "-ne apple" "tw- pears" "thr-e bananas"
> str_replace_all(fruits, "[aeiou]", "-")
[1] "-n- -ppl-" "tw- p--rs" "thr-- b-n-n-s"
str_replace_na(string, replacement = "NA")
> str_replace_na(c(NA, "abc", "def"))
[1] "NA" "abc" "def"
str_locate(string, pattern)
str_locate_all(string, pattern)
> fruit <- c("apple", "banana", "pear", "pineapple")
> str_locate(fruit, "a") # 在每个字符串中a的位置
start end
[1,] 1 1
[2,] 2 2
[3,] 3 3
[4,] 5 5
> str_locate_all(fruit, "a") # 在每个字符串中所有a的位置
[[1]]
start end
[1,] 1 1
[[2]]
start end
[1,] 2 2
[2,] 4 4
[3,] 6 6
[[3]]
start end
[1,] 3 3
[[4]]
start end
[1,] 5 5
str_extract(string, pattern)
str_extract_all(string, pattern, simplify = FALSE)
> shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2")
> str_extract(shopping_list, "\\d")
[1] "4" NA NA "2"
> str_extract_all(shopping_list, "\\d")
[[1]]
[1] "4"
[[2]]
character(0)
[[3]]
character(0)
[[4]]
[1] "2"
> str_extract_all(shopping_list, "\\d", simplify = TRUE)
[,1]
[1,] "4"
[2,] ""
[3,] ""
[4,] "2"
str_conv: 字符编码转换 str_to_upper: 字符串转成大写 str_to_lower: 字符串转成小写,规则同str_to_upper str_to_title: 字符串转成首字母大写,规则同str_to_upper
str_conv(string, encoding)
> x <- charToRaw('你好')
> x
[1] e4 bd a0 e5 a5 bd
> str_conv(x, "GBK")
[1] "你好"
> str_conv(x, "GB2312")
[1] "你好"
str_to_upper(string, locale = "en")
str_to_lower(string, locale = "en")
str_to_title(string, locale = "en")
str_to_sentence(string, locale = "en")
> dog <- "The quick brown dog"
> str_to_upper(dog)
[1] "THE QUICK BROWN DOG"
> str_to_lower(dog)
[1] "the quick brown dog"
> str_to_title(dog)
[1] "The Quick Brown Dog"
> str_to_sentence("the quick brown dog")
[1] "The quick brown dog"