get first part of a string in R [duplicate] - r

This question already has answers here:
Extract part of string before the first semicolon
(4 answers)
Closed 7 years ago.
I have a string "00_123.txt". I want to get first part of the string before ".", that is simply "00_123"
I found the substring("00_123.txt", 0, stop) can do it. But the problem is that I don't know where to stop, because the length of "00_123" can be changed.
How can I do that?

x <- "00_123.txt"
gsub("\\..*$", "", x)
[1] "00_123"

Related

Only remove open parentheses in R [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
How can I remove text within parentheses with a regex?
(9 answers)
Closed 3 years ago.
I need to remove a single closed parentheses from a string to fix an edge case in a simpler regex problem.
I need to remove text from within parentheses, but the solution I am currently using doesn't handle an extra single closed parentheses well. Should I use a different approach or can I add an extra step to handle this case?
Below is an example where all answers should be brother & I highlighted the line that it fails on below
cleaner = function(x){
x = tolower(x)
## if terms are in brackets - assume this is an alternative and remove
x = stringr::str_remove_all(x, "\\(.*\\)")
## if terms are seperated by semi-colons or commas, take the first, assume others are alternatives and remove
x = gsub("^(.*?)(,|;).*", "\\1", x)
## remove whitespace
x = stringi::stri_replace_all_charclass(x, "\\p{WHITE_SPACE}", "")
x
}
cleaner("brother(bro)")
cleaner("brother;bro")
cleaner("bro ther")
cleaner("(bro)brother ;bro")
cleaner("(bro)brother ;bro)") ## this fails
cleaner("(bro)brother ;(bro") # this doesnt
stringr::str_remove_all("(bro)brother ;bro)", "\\(.*\\)")
Thanks,
Sam

R - substring by number [duplicate]

This question already has answers here:
Extracting numbers from vectors of strings
(12 answers)
Closed 3 years ago.
what is the most easiest way how to get number from string? I have huge list of links like this, I need to get that number 98548 from it.
https://address.com/admin/customers/98548/contacts
Note that number cant have different count of numbers and can start from 0 to 9
This is the most easiest that I know :
str <- "https://address.com/admin/customers/98548/contacts"
str_extract_all(str, "\\d+")[[1]]
Using stringr:
no="https://address.com/admin/customers/98548/contacts"
unlist(stringr::str_extract_all(no,"\\d{1,}"))
[1] "98548"

R How do i split a string into a vector so that each place in the vector corresponds to a letter [duplicate]

This question already has answers here:
Split a character vector into individual characters? (opposite of paste or stringr::str_c)
(4 answers)
Closed 4 years ago.
I am trying to split a string let's say "abcde" into a vector of "a","b","c","d","e"
How can i do that?
i have tried strsplit but that makes it into 1 element
a=unlist(strsplit("abcde", split=" "))
We need the split = ""
unlist(strsplit('abcde', ''))

Remove characters in string before specific symbol(including it) [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Use gsub remove all string before first white space in R
(4 answers)
Closed 5 years ago.
at the beginning, yes - simillar questions are present here, however the solution doesn't work as it should - at least for me.
I'd like to remove all characters, letters and numbers with any combination before first semicolon, and also remove it too.
So we have some strings:
x <- "1;ABC;GEF2"
y <- "X;EER;3DR"
Let's do so gsub() with . and * which means any symbol with occurance 0 or more:
gsub(".*;", "", x)
gsub(".*;", "", y)
And as a result i get:
[1] "GEF2"
[1] "3DR"
But I'd like to have:
[1] "ABC;GEF2"
[1] "EER;3DR"
Why did it 'catch' second occurence of semicolon instead of first?
You could use
gsub("[^;]*;(.*)", "\\1", x)
# [1] "ABC;GEF2"

How to Reduce with paste function with different seperator [duplicate]

This question already has answers here:
Concatenate a vector of strings/character
(8 answers)
Closed 4 years ago.
For the following operation:
avector<-c("p1","p2","p3")
Reduce(paste,avector)
## "p1 p2 p3"
I want to get "p1.p2.p3"
Which is applying the paste function in Reduce with separator "."
Please advice.
Try this paste(avector,collapse = ".")

Resources