conditionally remove leading or trailing `.` character in R
Use regular expressions:
test <- c('.name.1.','name.2','.name.3.')
gsub('^\\.|\\.$', '', test)
# [1] "name.1" "name.2" "name.3"
The two backslashes, \\
, in the regular expression escape the dot, .
, which would actually mean any character. The caret, ^
, marks the beginning of the string, the dollar, $
, the end of the string. The pipe, |
, is a logical "or". So in essence the regular expression matches a dot at the beginning of the string or a dot at the end of the string and replaces it with an empty string.
More information on regular expressions can be found here and information on gsub and related functions here.
conditionally remove first character
We can use str_remove
to match the letter 'D' at the start (^
) of the string
library(dplyr)
library(stringr)
DF %>%
mutate(code2 = str_remove(code, '^D'))
Or in base R
with sub
DF$code2 <- sub('^D', '', DF$code)
Remove first character of string with condition in R
We can specify the ^0+
as pattern i.e. one or more 0s at the start (^
) of the string instead of .
(.
in regex matches any character)
data_individual$Zipcode <- sub("^0+", "", data_individual$Zipcode)
Or with tidyverse
library(stringr)
data_individual$Zipcode <- str_remove(data_individual$Zipcode, "^0+")
Another option without regex would be to convert to numeric
as numeric values doesn't support prefix 0 (assuming all zipcodes include only digits)
data_individual$Zipcode <- as.numeric(data_individual$Zipcode)
Remove trailing /'
How about this?
my.url <- "http://test.test/test.test.html/"
gsub("/$", "", my.url)
it returns
[1] "http://test.test/test.test.html"
you can use $
to look for elements at the end of the line
Remove character based on condition and position in string - R
While akrun's approach is the one that should be used. Here is stringr
composition:
- with
word(df, 1)
we take the left part of the string - with
word(df, -1)
we take the right part (here we use
2a.str_remove_all
with regex^0+
to remove leading zeros. - Finally we use
str_c
to combine both parts:
library(stringr)
str_c(word(df,1), str_remove_all(word(df, -1), '^0+'))
[1] "2015808" "201341" "20155" "2015301585" "2015311585" "2014380096" "2013100041"
R remove first character from string
If we need to remove the first character, use sub
, match one character (.
represents a single character), replace it with ''
.
sub('.', '', listfruit)
#[1] "applea" "bananab" "ranggeo"
Or for the first and last character, match the character at the start of the string (^.
) or the end of the string (.$
) and replace it with ''
.
gsub('^.|.$', '', listfruit)
#[1] "apple" "banana" "rangge"
We can also capture it as a group and replace with the backreference.
sub('^.(.*).$', '\\1', listfruit)
#[1] "apple" "banana" "rangge"
Another option is with substr
substr(listfruit, 2, nchar(listfruit)-1)
#[1] "apple" "banana" "rangge"
Conditionally removing from array if last character is []
You can remove the unwanted tickers using regex.
For example, with grep
:
stocktickers = c("PETR4", "VALE3", "MDNE3","BDLL4F ","QCOM34F", "SANB11","USIM5")
grep("(F|\\s)$",stocktickers, value = TRUE, invert = TRUE)
#[1] "PETR4" "VALE3" "MDNE3" "SANB11" "USIM5"
This removes the values from stocktickers
where it ends either with "F"
or whitespace.
remove last two characters only if it equals a condition
Try this
str_replace_all(strings, 'sl$','')
Related Topics
Ggplot2: How to Rotate a Graph in a Specific Angle
How to Render Custom Map Tiles Created with Gdal2Tiles in Leaflet for R
R - Converting Posixct to Milliseconds
Using Ggplot2 with Columns That Have Spaces in Their Names
Mapping Variable to Hexagon Size with Geom_Hex
Dependent Inputs in Shiny Application with R
Terms of a Sum in a R Expression
How to Read Large Numbers Precisely in R and Perform Arithmetic on Them
Creating Sequence of Dates for Each Group in R
How to Custom or Display Modebar in Plotly
R - Pivoting Duplicate Rows into Multiple Column with Unknown Number of Columns
How to Create a Dropdown List in a Shiny Table Using Datatable When Editing the Table
As.Date Produces Unexpected Result in a Sequence of Week-Based Dates