1、gsub
################################################################
R语言字符串替换
1、gsub( )
> gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
#解释:其中pattern是要替换的字符,replacement是替换的字符,x是对应的string或string vector。ignore.case表示是否忽视大小写。
##1、举例如下:
> x <- c("R Tutorial","PHP Tutorial", "HTML Tutorial")
> gsub("Tutorial","Examples",x)
[1] "R Examples" "PHP Examples" "HTML Examples"
##2、举例vector:
> x <- c("R Tutorial","PHP Tutorial", "HTML Tutorial")
> gsub("Tutorial","Examples",x)
[1] "R Examples" "PHP Examples" "HTML Examples"
##3、举例
> 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"
##4、举例
> x <- "line 4322: He is now 25 years old, and weights 130lbs"
> y <- gsub("[[:lower:]]","-",x)
> y
[1] "---- 4322: H- -- --- 25 ----- ---, --- ------- 130---"
2、tolower( )将大写改成小写
#tolower() function converter string to its lower case.
#tolower(x)##x: character vector
> tolower("EndMemo R Tutorial")
[1] "endmemo r tutorial"
> x <- c("Green", "Red", "Black")
> tolower(x)
[1] "green" "red" "black"
3、分隔符"_"去代替"\."
##斜杠是反斜杠,去除了;后面相当于点.
##所以,就用"_"代替"."
网友评论