STRING MANIPULATION
grep()
The call grep(pattern,x) searches for a specified substring pattern in a vector x of strings. If x has n elements—that is, it contains n strings—then grep(pattern,x) will return a vector of length up to n. Each element of this vector will be the index in x at which a match of pattern as a substring of x[i]) was found.
grep("Pole",c("Equator","North Pole","South Pole"))
## [1] 2 3
grep("pole",c("Equator","North Pole","South Pole"))
## integer(0)
In the first case, the string “Pole” was found in elements 2 and 3 of the second argument, hence the output (2,3). In the second case, string “pole” was not found anywhere, so an empty vector was returned.
nchar() The call nchar(x) finds the length of a string x.
nchar("South Pole")
## [1] 10
paste()
The call paste(…) concatenates several strings, returning the result in one long string.
paste("North","Pole")
## [1] "North Pole"
paste("North","Pole",sep="")
## [1] "NorthPole"
paste("North","Pole",sep=".")
## [1] "North.Pole"
paste("North","and","South","Poles")
## [1] "North and South Poles"
The optional argument sep can be used to put something other than a space between the pieces being spliced together. If I specify sep as an empty string, the pieces won’t have any character between them.
Concatenate “Final” Exam” using the steps shown above.
paste("final","Exam")
## [1] "final Exam"
paste("Final","Exam",sep="")
## [1] "FinalExam"
paste("Final","Exam",sep=".")
## [1] "Final.Exam"
paste("Final","and","Midterm","Exam")
## [1] "Final and Midterm Exam"
substr()
The call substr(x,start,stop) returns the substring in the given character position range start:stop in the given string x.
substring("Equator",3,5)
## [1] "uat"
strsplit()
The call strsplit(x,split) splits a string x into an R list of substrings based on another string split in x.
strsplit("6-16-2011",split="-")
## [[1]]
## [1] "6" "16" "2011"
Used the function above to split “11-28-2022”
strsplit("11-28-2022",split="-")
## [[1]]
## [1] "11" "28" "2022"