String manipulation functions in R ( part 1)
I wanted to do this quick tutorial because of an observation I made while working with some New R users who struggle with operations that does not involve numbers, operations and statistical calculations.
Handling text, images, files of all formats, are operations made possible within R via its numerous packages.
This time I started with basic strings, and I will probably add to this series later.
We will learn 4 new functions:
- grep
- grepl
These 2 function searches for matches of a given string variable, within each element of a character vector. the only difference between them is the output. The first one gives the position of the string that matches the search, the second one gives a logical result of TRUE or FALSE for all the strings of the vector.
- gsub
This function performs a substitution of a given string by an other in all the strings given as input for the function.
- str_replace
Let's start!
Here we input some text into a vector:
We will start with "grep" , This function will return the position of the string we are looking for in the vector, if it exist :
Now we will load a data-set to use in our next example, it is the "mtcars" data-set:
Here we want to have a list of all the cars that start with the letter "M" :
Finally, we will try to substitute a string with an other one in the case of a character vector, and the row names of a data frame:
I hope it was helpful!
Bellow the code I used to test these functions: