- 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"
推薦閱讀
- 會(huì)聲會(huì)影X5視頻剪輯高手速成
- 錯(cuò)覺(jué):AI 如何通過(guò)數(shù)據(jù)挖掘誤導(dǎo)我們
- ROS機(jī)器人編程與SLAM算法解析指南
- 小型電動(dòng)機(jī)實(shí)用設(shè)計(jì)手冊(cè)
- Kubernetes for Serverless Applications
- HTML5 Canvas Cookbook
- 深度學(xué)習(xí)與目標(biāo)檢測(cè)
- INSTANT Munin Plugin Starter
- 云計(jì)算和大數(shù)據(jù)的應(yīng)用
- 計(jì)算機(jī)組成與操作系統(tǒng)
- AI的25種可能
- Xilinx FPGA高級(jí)設(shè)計(jì)及應(yīng)用
- 51單片機(jī)應(yīng)用程序開(kāi)發(fā)與實(shí)踐
- EDA技術(shù)及其創(chuàng)新實(shí)踐(Verilog HDL版)
- 百度智能小程序:AI賦能新機(jī)遇