R語(yǔ)言gsub替換字符工具的具體使用
gsub()可以用于字段的刪減、增補(bǔ)、替換和切割,可以處理一個(gè)字段也可以處理由字段組成的向量。
具體的使用方法為:gsub("目標(biāo)字符", "替換字符", 對(duì)象)
在gsub函數(shù)中,任何字段處理都由將“替換字符”替換到“目標(biāo)字符”這一流程中實(shí)現(xiàn),令替換字符為''''可實(shí)現(xiàn)刪除,令替換字符為"目標(biāo)字符+增補(bǔ)內(nèi)容"可實(shí)現(xiàn)增補(bǔ),替換和切割也是使用類(lèi)似的操作。
> text <- "AbcdEfgh . Ijkl MNM"
> gsub("Efg", "AAA", text) #將Efg改為AAA,區(qū)分大小寫(xiě)
[1] "AbcdAAAh . Ijkl MNM"
任何符號(hào),包括空格、Tab和換行都是可以識(shí)別的
> gsub(" I", "i", text) #可識(shí)別空格
[1] "AbcdEfgh .ijkl MNM"
同時(shí)字符可以識(shí)別多個(gè),進(jìn)行批量置換
> gsub("M", "N", text)
[1] "AbcdEfgh . Ijkl NNN"
除此之外,gsub還有其他批量操作的方法
> gsub("^.* ", "a", text) #開(kāi)頭直到最后一個(gè)空格結(jié)束替換成a
[1] "aMNM"
> gsub("^.* I(j).*$", "\\1", text) #只保留一個(gè)j
[1] "j"
> gsub(" .*$", "b", text) #第一個(gè)空格直達(dá)結(jié)尾替換成b
[1] "AbcdEfghb"
> gsub("\\.", "\\+", text) #句號(hào).和加號(hào)+是特殊的,要添加\\來(lái)識(shí)別
[1] "AbcdEfgh + Ijkl MNM"
| Syntax | Description |
| \\d | Digit, 0,1,2 ... 9 |
| \\D | Not Digit |
| \\s | Space |
| \\S | Not Space |
| \\w | Word |
| \\W | Not Word |
| \\t | Tab |
| \\n | New line |
| ^ | Beginning of the string |
| $ | End of the string |
| \ | Escape special characters, e.g. \\ is "\", \+ is "+" |
| | | Alternation match. e.g. /(e|d)n/ matches "en" and "dn" |
| • | Any character, except \n or line terminator |
| [ab] | a or b |
| [^ab] | Any character except a and b |
| [0-9] | All Digit |
| [A-Z] | All uppercase A to Z letters |
| [a-z] | All lowercase a to z letters |
| [A-z] | All Uppercase and lowercase a to z letters |
| i+ | i at least one time |
| i* | i zero or more times |
| i? | i zero or 1 time |
| i{n} | i occurs n times in sequence |
| i{n1,n2} | i occurs n1 - n2 times in sequence |
| i{n1,n2}? | non greedy match, see above example |
| i{n,} | i occures >= n times |
| [:alnum:] | Alphanumeric characters: [:alpha:] and [:digit:] |
| [:alpha:] | Alphabetic characters: [:lower:] and [:upper:] |
| [:blank:] | Blank characters: e.g. space, tab |
| [:cntrl:] | Control characters |
| [:digit:] | Digits: 0 1 2 3 4 5 6 7 8 9 |
| [:graph:] | Graphical characters: [:alnum:] and [:punct:] |
| [:lower:] | Lower-case letters in the current locale |
| [:print:] | Printable characters: [:alnum:], [:punct:] and space |
| [:punct:] | Punctuation character: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ |
| [:space:] | Space characters: tab, newline, vertical tab, form feed, carriage return, space |
| [:upper:] | Upper-case letters in the current locale |
| [:xdigit:] | Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f |
到此這篇關(guān)于R語(yǔ)言gsub替換字符工具的具體使用的文章就介紹到這了,更多相關(guān)R語(yǔ)言gsub替換字符工具內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
R語(yǔ)言關(guān)于變量的知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于R語(yǔ)言關(guān)于變量的知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03
R語(yǔ)言可視化ggplot2繪制24小時(shí)動(dòng)態(tài)血糖圖
這篇文章主要為大家介紹了R語(yǔ)言可視化使用ggplot2繪制24小時(shí)動(dòng)態(tài)血糖,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
在R語(yǔ)言中實(shí)現(xiàn)Logistic邏輯回歸的操作
這篇文章主要介紹了在R語(yǔ)言中實(shí)現(xiàn)Logistic邏輯回歸的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
R語(yǔ)言RcppEigen計(jì)算點(diǎn)乘與矩陣乘法連乘算法錯(cuò)誤解決
這篇文章主要為大家介紹了RcppEigen計(jì)算點(diǎn)乘與矩陣乘法時(shí)發(fā)生連乘計(jì)算錯(cuò)誤的解決方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11
R語(yǔ)言數(shù)據(jù)可視化繪圖Dot plot點(diǎn)圖畫(huà)法示例
這篇文章主要為大家介紹了R語(yǔ)言數(shù)據(jù)可視化繪圖Dot plot點(diǎn)圖的畫(huà)法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02

