writer_typer
writer_typer

Reputation: 798

How to remove everything before a set of certain characters (e.g., "? - ")?

I would like to remove everything before "? - " including the characters themselves (question mark, space, hyphen, space). I know it is possible to remove everything before a particular character. I don't want to remove everything before just the hyphen - , as I have other statements that have hyphenated words. I tried this, but it didn't work for hyphenated words.

Example:

gsub(".*-", "", "To what extent do you disagree or agree with the following statements? - Statistics make me cry.")
gsub(".*-", "", "To what extent do you disagree or agree with the following statements? - T-tests are easy to interpret.")

Output:
" Statistics make me cry."
"tests are easy to interpret."

I would like the second statement to appear as T-tests are easy to interpret

Upvotes: 1

Views: 539

Answers (3)

m4D_guY
m4D_guY

Reputation: 198

If you want to use stringr in the tidyverse:

library(stringr)
str <- c("To what extent do you disagree or agree with the following statements? - Statistics make me cry.",
         "To what extent do you disagree or agree with the following statements? - T-tests are easy to interpret.")
str_split(str, ' - ', simplify = T)[,2]

Upvotes: 0

Eric Krantz
Eric Krantz

Reputation: 2199

You can try package stringr and use str_split. But it puts the results in a list so you have to pull the second element from the first list. Or start you input as a vector.

library(stringr)

str1 <- "To what extent do you disagree or agree with the following statements? - Statistics make me cry."
str2 <- "To what extent do you disagree or agree with the following statements? - T-tests are easy to interpret."

str_split(str1, pattern = "`? - ")[[1]][2]

[1] "Statistics make me cry."

str_split(str2, pattern = "`? - ")[[1]][2]

[1] "T-tests are easy to interpret."

Upvotes: 1

akrun
akrun

Reputation: 887991

Here sub would be enough instead of global g. Change the pattern to match the ? (metacharacter - so it is escaped \\), followed by zero or more spaces (\\s*) followed by the - and then zero or more spaces, replace with blank ('')

sub(".*\\?\\s*-\\s*", "", v1)
#[1] "Statistics make me cry."        "T-tests are easy to interpret."

data

v1 <- c("To what extent do you disagree or agree with the following statements? - Statistics make me cry.", 
"To what extent do you disagree or agree with the following statements? - T-tests are easy to interpret."
)

Upvotes: 1

Related Questions