0

I have a lot of records like "LISTA CIVICA | blablabla". They are character-class under the column "partito". I need to cut off the "| bla bla bla" in order to obtain for all the records just "LISTA CIVICA".

I need to obtain only LISTA CIVICA for all those records.

I tried this code but it does not work

gsub(pattern="",replacement = "LISTA CIVICA",ammcom$partito)

4 Answers 4

2

We can use sub to match zero or more spaces (\\s*) followed by the | (escape it as it is a metacharacter for OR (|) followed by other characters (.*) and replace it with blank ("")

sub("\\s*\\|.*", "", str1)
#[1] "LISTA CIVICA" "LISTA CIVICA"

Or another option is regmatches/regexpr

trimws(regmatches(str1, regexpr("^[^|]+", str1)))
#[1] "LISTA CIVICA" "LISTA CIVICA"

data

str1 <- c("LISTA CIVICA | INSIEME PER ALBERA", "LISTA CIVICA | blablabla")
Sign up to request clarification or add additional context in comments.

5 Comments

I need to change all the record in that way...I tried but it is not still working...
@VitoFabrizioBrugnola You need to change the str1 with ammcom$partito. Also, this is based on the example you showed
I reforumlate: I have a lot of records like LISTA CIVICA | blablabla. They are character-class under the column "Partito". I need to cut off the "| bla bla bla" in order to obtain for all the records just "LISTA CIVICA".
@VitoFabrizioBrugnola You need to update your post. Others won't know what your pattern will be if you just show one example
@VitoFabrizioBrugnola That is what the regex is doing to match the | followed by any characters and replace it with blank
2

Another way could be using lookaround expression:

library(stringr)
trimws(str_replace_all(text,"\\|(?>.*)",""))

OR

trimws(str_replace_all(text,"\\|.*",""))

Output:

 > trimws(str_replace_all(text,"\\|.*",""))
[1] "LISTA CIVICA" "LISTA CIVICA"

Input data:

text = c("LISTA CIVICA | INSIEME PER ALBERA","LISTA CIVICA | bla blabla")

Comments

1

A friend of mine found out how to fix my problem.

length(ammcom$partito[grep("^LISTA",ammcom$partito)])
L <- rep("LISTA CIVICA", 92033)
ammcom$partito[grep("^LISTA",ammcom$partito)] <- L

Comments

0

If the string you want to keep is always the before the |, you can also split the string around | and retain only the first element:

str1 <- "LISTA CIVICA | INSIEME PER ALBERA"

unlist(lapply(strsplit(str1,"\\|"), function(x) x[[1]]))

1 Comment

Sorry man, i corrected my questione, I was not so cleare at the first attempt (my first question on Stack's)...be patient, please! ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.