zoukankan      html  css  js  c++  java
  • 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%返回的是逻辑值。

  • 相关阅读:
    只打开一次浏览器,生成html测试报告<小紧张中......>
    hadoop学习第四天-Writable和WritableComparable序列化接口的使用&&MapReduce中传递javaBean的简单例子
    hadoop学习第三天-MapReduce介绍&&WordCount示例&&倒排索引示例
    hadoop学习第二天-了解HDFS的基本概念&&分布式集群的搭建&&HDFS基本命令的使用
    hadoop学习第一天-hadoop初步环境搭建&伪分布式计算配置(详细)
    大三了,写写博客
    方向问题
    第一天注册博客
    jquery跨域请求事例
    ECharts学习记录
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7354701.html
Copyright © 2011-2022 走看看