R语言字符串替换
R gsub Function
gsub() function replaces all matches of a string, if the parameter is a string vector, returns a string vector of the same length and with the same attributes (after possible coercion to character). Elements of string vectors which are not substituted will be returned unchanged (including any declared encoding).
gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
• pattern: string to be matched
• replacement: string for replacement
• x: string or string vector
• ignore.case: if TRUE, ignore case
...
> x <- "R Tutorial"
> gsub("ut","ot",x)
[1] "R Totorial"
Case insensitive replace:
> gsub("tut","ot",x,ignore.case=T))
[1] "R otorial"
If ignore.case is not set to True, no replace take place:
> gsub("tut","ot",x)
[1] "R Tutorial"
> x <- "line 4322: He is now 25 years old, and weights 130lbs"
> y <- gsub("\\d+","---",x)
> y
[1] "line ---: He is now --- years old, and weights ---lbs"
> x <- "line 4322: He is now 25 years old, and weights 130lbs"
> y <- gsub("[[:lower:]]","-",x)
> y
[1] "---- 4322: H- -- --- 25 ----- ---, --- ------- 130---"
Vector replacement:
> x <- c("R Tutorial","PHP Tutorial", "HTML Tutorial")
> gsub("Tutorial","Examples",x)
[1] "R Examples" "PHP Examples" "HTML Examples"
Regular Expression Syntax:
| 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 |
REF:
http://www.endmemo.com/program/R/gsub.php
http://cran.r-project.org/web/packages/stringr/stringr.pdf
http://stackoverflow.com/questions/11936339/in-r-how-do-i-replace-text-within-a-string
R语言字符串替换的更多相关文章
- R语言字符串函数
字符串长度: nchar("hello world") #字符串连接:paste) <- value substr("abcdef", 2, 4)[1] ...
- C语言字符串替换
void exchg(char * str) { if(str == NULL) return; int len = strlen(str); char tmp; for(int i=0,j=len- ...
- R语言的字符串处理
R语言字符串的拼接 content<-paste("124235","789","124",sep="@") 运行 ...
- Rserve详解,R语言客户端RSclient【转】
R语言服务器程序 Rserve详解 http://blog.fens.me/r-rserve-server/ Rserve的R语言客户端RSclient https://blog.csdn.net/u ...
- R语言学习笔记:字符串处理
想在R语言中生成一个图形文件的文件名,前缀是fitbit,后面跟上月份,再加上".jpg",先不百度,试了试其它语言的类似语法,没一个可行的: C#中:"fitbit&q ...
- R语言︱文本(字符串)处理与正则表达式
处理文本是每一种计算机语言都应该具备的功能,但不是每一种语言都侧重于处理文本.R语言是统计的语言,处理文本不是它的强项,perl语言这方面的功能比R不知要强多少倍.幸运的是R语言的可扩展能力很强,DN ...
- R语言中的字符串处理函数
内容概览 尽管R是一门以数值向量和矩阵为核心的统计语言,但字符串有时候也会在数据分析中占到相当大的份量. R语言是一个擅长处理数据的语言,但是也不可避免的需要处理一些字符串(文本数据).如何高 ...
- R语言学习 第五篇:字符串操作
文本数据存储在字符向量中,字符向量的每个元素都是字符串,而非单独的字符.在R中,可以使用双引号,或单引号表示字符. 一,字符串中的字符数量 函数nchar()用于获得字符串中的字符数量: > s ...
- 【R笔记】R语言中的字符串处理函数
内容概览 尽管R是一门以数值向量和矩阵为核心的统计语言,但字符串同样极为重要.从医疗研究数据里的出生日期到文本挖掘的应用,字符串数据在R程序中使用的频率非常高.R语言提供了很多字符串操作函数,本文仅简 ...
随机推荐
- MySQL,如何修改root帐户密码、如何解决root帐户忘记密码的问题
1. 如何修改root帐户密码 打开MySQL 5.6 Command Line Client窗口,输入当前密码登录,然后依次输入如下3条命令: use mysql; update user set ...
- node调试的两种方法
刚开始学node.js的时候,一直在用node-inspector,虽然很麻烦,但聊胜于无.后面公司牛人推荐使用node-webkit,就再也没用过node-inspector.再后来node.js版 ...
- scikit-learn:6. Strategies to scale computationally: bigger data
參考:http://scikit-learn.org/stable/modules/scaling_strategies.html 对于examples.features(或者两者)数量非常大的情况, ...
- 菜鸟学Java(十六)——Jboss简介
简介 JBoss是全世界开发者共同努力的成果,一个基于J2EE的开放源代码的应用服务器. 因为JBoss代码遵循LGPL许可,可以在任何商业应用中免费使用它,而不用支付费用.2006年,Jboss公司 ...
- Linux查看系统cpu个数、核心书、线程数
现在cpu核心数.线程数越来越高,本文将带你了解如何确定一台服务器有多少个cpu.每个cpu有几个核心.每个核心有几个线程. 工具/原料 Linux服务器 方法/步骤 查看物理cpu个数 grep ...
- C#如何删除数组中的一个元素
C#如何删除数组中的一个元素,剩余的元素组成新数组,数组名不变double[] arr = new double[n];需要删除的是第m+1个数据arr[m]求新数组arr.(新数组arr包含n-1个 ...
- Python与操作系统有关的模块
Os模块 Python的标准库中的os模块主要涉及普遍的操作系统功能.可以在Linux和Windows下运行,与平台无关.os.sep 可以取代操作系统特定的路径分割符.os.name字符串指示你正在 ...
- JAVA-JSP内置对象之session对象设置并获得session生命周期
相关资料:<21天学通Java Web开发> session对象设置并获得session生命周期1.通过session对象的setMaxInactiveInterval()方法可以设置se ...
- java 获取 path
(1).request.getRealPath("/");//不推荐使用获取工程的根路径 (2).request.getRealPath(request.getRequestURI ...
- 带网上开户表单jQuery焦点图
带网上开户表单jQuery焦点图是一款适合证券公司的带表单的图片左右滚动切换特效代码.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class="ind ...