Remove Characters Before or After Point in String in R (Example)
This article shows how to delete characters in a character string before or after a point in the R programming language.
The page is structured as follows:
Let’s start right away!
Creation of Example Data
The following data will be used as basement for this R tutorial:
x <- "aaaa.bbbbbb" # Create example data x # Print example data # [1] "aaaa.bbbbbb"
The previous output of the RStudio console shows the structure of the example data – It’s a single character string containing the letters a and b and a point or dot (i.e. “.”) in the middle.
Example 1: Remove Part After . Using gsub() Function and \\
This example explains how to extract only the part of a character string before or after a point.
Let’s first apply the gsub function as we usually would, in case we want to remove the part of a string before or after a pattern:
gsub("..*", "", x) # Apply gsub without \\ # [1] ""
As you can see, the RStudio console returns an empty character after running the previous R code.
The reason for this is that the symbol . is considered as a special character. For that reason, we have to use a double backslash in front of the point (i.e. \\).
gsub("\\..*", "", x) # Apply gsub with \\ # [1] "aaaa"
This works as expected!
Example 2: Remove Part Before . Using gsub() Function and \\
It is also possible to remove all characters in front of a point using the gsub function.
For this task, we can use the R code below:
gsub(".*\\.", "", x) # Apply gsub with \\ # [1] "bbbbbb"
Looks good!
Video, Further Resources & Summary
In case you need more info on the R programming codes of this article, you may watch the following video which I have published on my YouTube channel. In the video, I’m explaining the R programming code of this article.
Besides the video, you may want to have a look at the other tutorials of this homepage. I have released numerous tutorials already:
- Extract Substring Before or After Pattern
- Extract First or Last n Characters from String
- Remove All Special Characters from String in R
- The R Programming Language
Summary: In this tutorial, I have explained how to remove characters before or after points in the R programming language. Let me know in the comments, if you have additional questions.
8 Comments. Leave new
You forgot to tell us how to remove the characters before a point.
Hey Girsão,
Thanks a lot for the hint, it seems like I have forgotten to do this when I created the tutorial.
I have just added another example, which explains how to delete all characters before a point.
Regards
Joachim
what if data is aaa.bbbb.cccc, and want to extract bbbb?
Hey,
This is probably not the most efficient solution, but it should work:
Regards,
Joachim
hi dear .. your classes are amazing .
if i have the below data frame ,,, how can i run the same command to remove FY from the Year column ? thank you
Identifier Company Name Country Year
ELSE.OQ Electro-Sensors Inc USA FY2021
ELSE.OQ Electro-Sensors Inc USA FY2020
ELSE.OQ Electro-Sensors Inc USA FY2019
ELSE.OQ Electro-Sensors Inc USA FY2018
ELSE.OQ Electro-Sensors Inc USA FY2017
ELSE.OQ Electro-Sensors Inc USA FY2016
ELSE.OQ Electro-Sensors Inc USA FY2015
ELSE.OQ Electro-Sensors Inc USA FY2014
ELSE.OQ Electro-Sensors Inc USA FY2013
ELSE.OQ Electro-Sensors Inc USA FY2012
ELSE.OQ Electro-Sensors Inc USA FY2011
ELSE.OQ Electro-Sensors Inc USA FY2010
ELSE.OQ Electro-Sensors Inc USA FY2009
ELSE.OQ Electro-Sensors Inc USA FY2008
ELSE.OQ Electro-Sensors Inc USA FY2007
ELSE.OQ Electro-Sensors Inc USA FY2006
ELSE.OQ Electro-Sensors Inc USA FY2005
ELSE.OQ Electro-Sensors Inc USA FY2004
ELSE.OQ Electro-Sensors Inc USA FY2003
Hey Nadal,
Thank you for such a nice feedback. You can check the code below.
For more detailed information, you can check our tutorial, Replace Specific Characters in String in R.
Regards,
Cansu
Can someone help me I am working with table that has number sepereted by semicolons I want to extract every number after semicolon here is my code below
P[i,j] <- as.numeric(gsub(";..*","", Mod2Data[i,j]))
Hello Luzuko,
It seems you want to extract numbers after semicolons. The code you provided extracts numbers before the first semicolon. Let’s modify your regex pattern to extract numbers after the semicolons.
Given that you have numbers separated by semicolons, I assume there can be multiple numbers after multiple semicolons. To extract all numbers following semicolons, you’d need a loop or some iterative approach.
Here’s a simple method using strsplit and lapply:
I hope this is close to what you want to do.
Best,
Cansu