Obtaining part of string before question mark [duplicate] - r

This question already has answers here:
Remove all text before colon
(10 answers)
Closed 5 years ago.
I've got a table of strings like i.e.:
START0001?sthEND1,
START002?sthEND2,
START03?sthEND3,
START4
How could I obtain the table:
START1,
START2,
START3,
START4
?
I can do this with
gsub(sub('^([^?]+)*','',napis),"",napis)
but there is a problem with "?" sign that stays after all.

Try this:
y<-"START0001?sthEND1"
x<-unlist(strsplit(y,""))
idx<-which(grepl("\\?",x))
x<-paste0(x[1:(idx-1)],collapse = "")
x<-gsub("0","",x)

Related

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"

How to I extract a list of items between curly brackets {} in a string? [duplicate]

This question already has answers here:
Learning Regular Expressions [closed]
(1 answer)
Extracting a string between other two strings in R
(4 answers)
Closed 4 years ago.
I have character strings like
str1 <- "Hello {can you please} {extract this}"
I need to extract "can you please" and "extract" this as a list with regex.
How can I do it?

Is there any way to turn a column into a string? [duplicate]

This question already has answers here:
How do I paste string columns in data.frame [duplicate]
(2 answers)
Closed 5 years ago.
For example, if I have a data set
data.frame(x = c("a","b","a","b","c"))
how can I get a output =
a,b,a,b,c in a list or text.
paste(data_frame,collapse=",") will collapse the column into a single string.

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 = ".")

get first part of a string in R [duplicate]

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"

Resources