R String Functions


R string functions include substr(x), nchar(x), toupper(x), tolower(x), strsplit(x,y),paste(...), and regular expression functions sub(...), grep(...) etc.

>s <- "EndMemo.com R Language Tutorial"
>substr(s,0,7)

[1] "EndMemo"


Get string length:

>nchar(s)

[1] 31


To uppercase:

>x <- toupper(s)
>x

[1] "ENDMEMO.COM R LANGUAGE TUTORIAL"


To lowercase:

>x <- tolower(s)
>x

[1] "endmemo.com r language tutorial"


Split the string at letter "o":

>x <- strsplit(s,"o")

[[1]]
[1] "EndMem" ".c" "m R Language Tut" "rial"


Concatenate two strings:

>x <- paste(x," -- String Functions",sep="")
>x

[1] "endmemo.com r language tutorial -- String Functions"


Substring replacement:

>x <- sub("Tutorial","Examples",s)
>x

[1] "EndMemo.com R Language Examples"


Use regular expression:

>x <- sub("n.+e","XXX",s)
>x

[1] "EXXX Tutorial"


Please see grep() function for more regular expression handling of string.




endmemo.com © 2024  | Terms of Use | Privacy | Home