Hello, I have a string and I want to extract all the text after the symbol ‘?’.
x<- “What is your name? Peter Parker”
how to use the str_extract_all function to do so?
Hello, I have a string and I want to extract all the text after the symbol ‘?’.
x<- “What is your name? Peter Parker”
how to use the str_extract_all function to do so?
I am not sure how to do this using the str_extract_all function but it can also be done using the read.table()
> x<- "What is your name? Peter Parker"
> read.table(text = x, sep = "?", as.is = TRUE)
V1 V2
1 What is your name Peter Parker
This gives a table with two columns V1 and V2.
Now to extract the second part after ?
> read.table(text = x, sep = "?", as.is = TRUE)$V2
[1] " Peter Parker"
@shuvayan: Inventive use of the read.table!
@adityashrm21, I don’t know why you wish to use the str_extract_all
for a string, as grep()
is more suited to this task.
see https://stat.ethz.ch/R-manual/R-devel/library/base/html/grep.html
regexpr returns an integer vector of the same length as text giving the starting position of the first match
now pass this as a value for substr and nchar
see here
x<- "What is your name? Peter Parker"
a=regexpr("\\?",x)
b=nchar(x)
substr(x,a+1,b)
b=nchar(x)
a=regexpr("\?",x)
substr(x,a,b)
[1] “? Peter Parker"
substr(x,a+1,b)
[1] " Peter Parker”