- Machine Learning with R Quick Start Guide
- Iván Pastor Sanz
- 285字
- 2021-06-24 16:01:30
String functions
Let's see how we can transform or convert strings using R.
The most relevant string examples are as follows:
- To know the number of characters in a string:
nchar(c)
## [1] 24
- To return the substring of x, originating at a particular character in x:
substring(c,4)
## [1] "s is our first string"
- To return the substring of x originating at one character located at n and ending at another character located at a place, m:
substring(c,1,4)
## [1] "This"
- To divide the string x into a list of sub chains using the delimiter as a separator:
strsplit(c, " ")
## [[1]]
## [1] "This" "is" "our" "first" "string"
- To check if the given pattern is in the string, and in that case returns true (or 1):
grep("our", c)
## [1] 1
grep("book", c)
## integer(0)
- To look for the first occurrence of a pattern in a string:
regexpr("our", c)
## [1] 9
## attr(,"match.length")
## [1] 3
## attr(,"index.type")
## [1] "chars"
## attr(,"useBytes")
## [1] TRUE
- To convert the string into lowercase:
tolower(c)
## [1] "this is our first string"
- To convert the string into capital letters:
toupper(c)
## [1] "THIS IS OUR FIRST STRING"
- To replace the first occurrence of the pattern by the given value with a string:
sub("our", "my", c)
## [1] "This is my first string"
- To replace the occurrences of the pattern with the given value with a string:
gsub("our", "my", c)
## [1] "This is my first string"
- To return the string as elements of the given array, separated by the given separator using paste(string,array, sep=“Separator”):
paste(c,"My book",sep=" : ")
## [1] "This is our first string : My book"
推薦閱讀
- OpenStack for Architects
- Linux Mint System Administrator’s Beginner's Guide
- 協(xié)作機(jī)器人技術(shù)及應(yīng)用
- Google App Inventor
- 流處理器研究與設(shè)計(jì)
- Maya 2012從入門到精通
- 永磁同步電動(dòng)機(jī)變頻調(diào)速系統(tǒng)及其控制(第2版)
- Embedded Programming with Modern C++ Cookbook
- 人工智能實(shí)踐錄
- 工業(yè)機(jī)器人運(yùn)動(dòng)仿真編程實(shí)踐:基于Android和OpenGL
- Cloud Security Automation
- Web編程基礎(chǔ)
- x86/x64體系探索及編程
- 暗戰(zhàn)強(qiáng)人:黑客攻防入門全程圖解
- Office 2010輕松入門