STRING MANIPULATION


Although R is a statistical language with numeric vectors and matrices playing a central role, character strings are surprisingly important as well. Ranging from birth dates stored in medical research data files to textmining applications, character data arises quite frequently in R programs. Accordingly, R has a number of string-manipulation utilities, many of which will be introduced in this chapter.

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.

Here’s an example of using grep:

#grep- looks for how many times the 'pattern' appears in the variables given; which in this case was the 2 and 3 values
grep("Pole",c("Equator","North Pole","South Pole"))
## [1] 2 3
#it is text case- sensative so even though it is the same wording, it is not written the same. So 'pole', since its not 'Pole' does not appear. 
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. Here’s an example:

#nchar- Finds the length of a string (which in this case is 10, because it includes spaces (9 letters, and 1 space))
nchar("South Pole")
## [1] 10

The string “South Pole” was found to have 10 characters. C programmers, take note: There is no NULL character terminating R strings. Also note that the results of nchar() will be unpredictable if x is not in character mode. For instance, nchar(NA) turns out to be 2, and nchar(factor(“abc”)) is 1. For more consistent results on nonstring objects, use Hadley Wickham’s stringr package on CRAN.

paste()

The call paste(…) concatenates several strings, returning the result in one long string. Here are some examples:

#paste()- is another way of concatenating several strings together with and without spaces. {using 'sep' to decided spaces}
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"

As you can see, the optional argument sep can be used to put something other than a space between the pieces being spliced together. If you specify sep as an empty string, the pieces won’t have any character between them.

Concatenate “Final” Exam” using the steps shown above.

# Enter Answer Here
paste("Final", "Exam")
## [1] "Final Exam"
paste("Final")
## [1] "Final"
paste("Exam")
## [1] "Exam"
paste("Final", "Exam", sep = "")
## [1] "FinalExam"

substr()

The call substr(x,start,stop) returns the substring in the given character position range start:stop in the given string x. Here’s an example:

#substr()- returns the certain value in the given positions. 
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. Here’s an example:

#Splits the string character by the variable its given. 
  #refering to excel, it splits data by ''. 
strsplit("6-16-2011",split="-")
## [[1]]
## [1] "6"    "16"   "2011"

Use the function above to split “11-28-2022”

#Enter Answer Here
strsplit("11-28-2022",split="-")
## [[1]]
## [1] "11"   "28"   "2022"