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:
SyntaxDescription
\\dDigit, 0,1,2 ... 9
\\DNot Digit
\\sSpace
\\SNot Space
\\wWord
\\WNot Word
\\tTab
\\nNew 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
R语言中,字符串是一个常用的数据类型,我们经常需要对字符串进行操作。其中一项常见的操作是在字符串中指定位置进行替换。下面就来介绍一下在R语言中如何实现这个操作。 首先需要了解的是,R语言中字符串是不可变的,也就是说一旦创建了一个字符串,就不能直接在其上进行修改,而只能通过生成新的字符串来实现修改。因此在进行字符串替换时,我们需要先将原始字符串分成需要替换部分的前缀、替换后的中缀和需要替换部分的后缀三部分,然后用新的字符串将这三部分拼接起来即可。 下面是一个简单的示例代码,演示了如何将一个字符串中指定位置的字符替换为其他字符: ```R str_replace_at <- function(str, index, replace) { prefix <- substr(str, 1, index - 1) suffix <- substr(str, index + 1, nchar(str)) paste0(prefix, replace, suffix) } # 例子 str_replace_at("hello world", 6, "R") # 输出 "hello Rorld" ``` 上述代码中,我们定义了一个str_replace_at函数,它的三个参数分别是原始字符串、需要替换的位置和替换后的字符。该函数的实现首先使用substr函数将原始字符串分成了前缀、中缀和后缀三部分,然后使用paste0函数将这三部分拼接起来,并返回了新字符串。 需要注意的是,上述示例代码中的替换位置是从1开始计算的,而不是从0开始。如果需要在R语言中实现从0开始计算的字符串替换,只需要将上述代码中的index - 1替换为index即可。 总之,在R语言中进行字符串指定位置替换的方法较为简单,只需要将原始字符串分成三部分并使用paste等函数进行拼接即可。同时要注意字符串非可变性,需要生成新字符串进行替换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值