How To Remove the First Character From String in R (With Examples)

Most people are initially drawn to R programming by its powerful statistical computing functionality. The language gives you everything you need to expertly create and manipulate complex collections of data. But make no mistake, R is a full programming language that can be used for almost anything seen with Python or Perl. In fact, you might be surprised to find how well R fits into tasks related to text manipulation. R can juggle the content of a text document just as easily as it does a collection of numbers. You can even do something as intricate as removing the first character from a string in R with very little effort.

Strings, R, and You

One of the great things about R is that it has a lot of powerful 3rd party libraries. Stringr in particular can help us work with some advanced string manipulation without needing to write equally complex code. But don’t underestimate what the base R language is capable of. Stringr and other libraries like stringi do make string manipulation a lot easier. But for a basic task, like trying to remove first character from a string? The standard R functionality is more than enough. In fact, that’s true in a very literal sense. The standard R library gives us multiple ways to approach character removal from a string.

Basic String Manipulation With Substring

Each method of string manipulation brings something new to the table. And each of the methods we’re about to look at can remove the first character from a string. But the easiest way to go about this process is to use the substring function. It’s not the most advanced method of string manipulation. But it’s the best for quick work like this. Take a look at the following code.

ourString <- “_The quick brown fox jumps over the lazy dog”
ourString <- substring(ourString, 2, nchar(ourString))

We begin by defining ourString with an unwanted character at the start. This isn’t too uncommon a phenomenon if we’re manually scraping data from, for example, an OCR program’s output. Many programs put notation at the beginning of output to signify a variety of different things. But in any case, the end result is that we want to remove that character from our string. And we need the method to be able to handle any other sentence we throw at it.

The following line shows how we can do that with substring. We pass ourString to it as the first argument. Next, we give it the number 2. Substring essentially works like a cut function in a multimedia editing system. We give it a start coordinate and an end point. Substring will then proceed to return a string containing the contents we specified. In this case we supply a 2 in order to indicate the second position within ourString. Note that the number refers to the first character that well be kept.

Substring will ignore anything before the first numeric argument. In this case that means that it will ignore the first character in our string. If we then pass a number corresponding to the last position within our string we’ll have essentially told substring to give us the entire string minus the first character.

Unfortunately, substring doesn’t give us any type of control character to specify that we want to select the end of the string. But we can manage the same thing with relative ease by using the nchar function. Nchar returns the number of characters in a vector. And because R starts counting at 1, the nchar return will be the numeric mark of the last character in any supplied string. So we simply need to put a call to nchar as the final argument in substring. With that in place, we’ve told substring to give us a new string containing everything in ourString except the first character. We then assign that back to ourString. The underlying functionality of the process might be somewhat complex. But that also shows how powerful R is. We got a lot of work out of a single line of code. But while this is the easiest method, it’s far from the only one.

Other Forms of Character Replacement

R also gives us access to regular expressions. This is a full topic unto itself. But regular expressions, also known as regex, are character sequences which signify more advanced search patterns. Regex provides us with a wealth of special characters and combinations that essentially act as shorthand for string selection. This selection system, in turn, fits neatly into string manipulation. Take a look at the following example to see how we can leverage the power of regular expressions.

ourString <- “_The quick brown fox jumps over the lazy dog”
ourString <- gsub(“^.”, “”, ourString)

We once again assign our previous sentence to ourString. But this time around we reassign ourString’s contents with a function called gsub. Gsub stands for global substitution. And yes, we can substitute a null value within gsub. Doing so will essentially remove the selected characters. But to do so we first need to actually select that character. We’re able to do so with the “^.”, “” expression. We create what amounts to a new string in order to initiate the expression as a single argument. The initial ^ character is shorthand to signal the beginning of a string. The following . serves as a wildcard value within regex.

The expression essentially tells gsub to go to the first character in ourString and select it. The use of a wildcard means that gsub will recognize a match with whatever’s in the first character position of a string. The second argument, two quotation marks, simply means that we’ll replace the selected data with nothing. In doing so we essentially delete the first character in the string. We proceed to assign the result back to ourString again.

But that’s not the end of our options. We could also use a function that shares traits with both substring and gsub – sub. Try running the following code to see sub in action.

ourString <- “_The quick brown fox jumps over the lazy dog”
ourString <- sub(“^.”, “”, ourString)

We’re able to use the same regex expression with sub that we did with gsub. And it gives us the same results. There are some significant differences between advanced usage cases for sub and gsub. But for simple character replacement they operate in a fairly similar manner. We just need to pass a pattern to match, a replacement value, and the name of the string we want to use as input.

Scroll to top
Privacy Policy