Replace character at certain location within string - r

Given a certain string, e.g., s = "tesX123", how can I replace a certain character at a certain location?
In this example, the character at position 4 should be changed to "t".
Does a method exist in the style of setChar(s, 4, "t") which would result in test123?

Try substr()
substr(s, 4, 4) <- "t"
> s
#[1] "test123"

We can use sub
sub("(.{3}).", "\\1t", s)
#[1] "test123"

Related

Comparing Two Strings And Changing Case of Differing Characters

I'm trying to compare a "master" string to a list of strings using R. Based on this comparison, I'd like all characters in the string which differ from the master string changed to lowercase.
For example: the master string is "AGG". The list of strings being compared to is ["ATT", "AGT"]. I want to return ["Att","AGt"]. Order also matters. So ["GGA"] should return ["gGa"].
Any help would be greatly appreciated!
You could turn all characters into lowercase first, and turn those characters in the master string back to uppercase.
master <- "AGG"
x <- c("ATT", "AGT", "GGA")
chartr(tolower(master), master, tolower(x))
# [1] "Att" "AGt" "GGA"
Update: If you want to compare x and master character-by-character, try this:
sapply(strsplit(x, ""), \(char) {
paste(ifelse(char == strsplit(master, "")[[1]], char, tolower(char)), collapse = "")
})
# [1] "Att" "AGt" "gGa"

R: remove substring and change the remaining string by addition of a number

in R: I have some strings with the following pattern of letters and numbers
A11B3XyC4
A1B14C23XyC16
B14C23XyC16D3
B14C23C16D3
I want to remove the part "Xy" (always the same letters) and when I do this I want to increase the number behind the Letter B by one (everything else should stay the same).
When there is no "Xy" in the string there is no change to the string
The result should look like this:
A11B4C4
A1B15C23C16
B15C23C16D3
B14C23C16D3
Could you point me to a function capable of this? I struggle with doing a calculation (x+1) with a string.
Thank you!
We could use str_replace to do the increment on the substring of numbers that follows the 'B' string after removing the 'Xy' only for cases where there is 'Xy' substring in case_when
library(stringr)
library(dplyr)
case_when(str_detect(str1, "Xy") ~ str_replace(str_remove(str1,
"Xy"), "(?<=B)(\\d+)", function(x) as.numeric(x) + 1), TRUE ~str1)
[1] "A11B4C4" "A1B15C23C16" "B15C23C16D3" "B14C23C16D3"
data
str1 <- c("A11B3XyC4", "A1B14C23XyC16", "B14C23XyC16D3", "B14C23C16D3")

Removing patterns in R

I have a character string that looks like below and I want to delete lines that doesn't have any value after '_'.
How do I do that in R?
[985] "Pclo_" "P2yr13_ S329" "Basp1_ S131"
[988] "Stk39_ S405" "Srrm2_ S351" "Grin2b_ S930"
[991] "Matr3_ S604" "Map1b_ S1781" "Crmp1_"
[994] "Elmo1_" "Pcdhgc5_" "Sp4_"
[997] "Pbrm1_" "Pphln1_" "Gnl1_ S33"
[1000] "Kiaa1456_"
We can use grep
grep("_$", v1, invert = TRUE, value = TRUE)
Or endsWith
v1[!endsWith(v1, "_")]
We can use substring to get the last character in the vector and select if it is not "_".
x <- c("Pclo_","P2yr13_ S329","Basp1_ S131")
x[substring(x, nchar(x)) != '_']
#[1] "P2yr13_ S329" "Basp1_ S131"
Last character can be extracted using regex as well with sub :
x[sub('.*(.)$', '\\1', x) != '_']

slicing and replacing a number in R

I have a number like this example:
fferc114
and would like to:
1- remove the first 3 elements
2- keep the 2nd 3 elements
the expected output would look like this:
expected output:
dfer**
I am trying to that in R using the following code but it does not return what I want. do you know how to fix it?
trying to that
You can try:
x <- "E431250000326"
paste0(substr(x, 4, 6), "-", substr(x, 11, nchar(x)))
[1] "125-326"
Or if you want to subtract the numbers:
as.numeric(substr(x, 4, 6)) - as.numeric(substr(x, 11, nchar(x)))
A regex approach
string <- "E431250000326"
sub(".{3}(.{3})(.{4})(.{3})", "\\1-\\3", string)
#[1] "125-326"
As described in the question this removes first 3 elements, selects (using capture group) next 3 elements, replaces next 4 elements with "-" and selects next 3 elements.
We can specifically match a digit to capture the group
sub(".{3}(\\d{3})\\d{4}(\\d{3})", "\\1-\\2", string)
#[1] "125-326"
data
string <- "E431250000326"

extracting character from character string as per certain conditions

Let's say
x = "R is so tough for SAS programmer"
y = "R why you so hard"
Now we have to find the word before 8th place and the first space (" ") encountered going right to left, i.e. backwards.
In case of x it would be the word "so"
In the case of y it would be "y"
How can I do this?
Here is another option with word and sub
library(stringr)
word(sub("^(.{1,7}).*", "\\1", x), -1)
#[1] "so" "y"
data
x <- c("R is so tough for SAS programmer", "R why you so hard")
Let's assume you have both strings in one vector:
x = c("R is so tough for SAS programmer", "R why you so hard")
Then, if I understand your question correctly, you can use a combination of substr to extract the first 7 characters of each string and then sub to extract the part after the last space:
sub(".*\\s", "", substr(x, 1, 7))
#[1] "so" "y"
It may be safer to use
sub(".*\\s", "", trimws(substr(x, 1, 7), "right"))
which will cut off any whitespace on the right side of the vector resulting from substr. This ensures that the sub call won't accidentally match a space at the end of the string.

Resources