i-box
i-box

Reputation: 47

Insert a character as an item delimiter into string in R

In R, I have a single string with multiple entries such as:

mydata <- c("(first data entry) (second data entry) (third data entry) ")

I want to insert the pipe symbol "|" between the entries as an item delimiter, ending up with the following list:

"(first data entry)|(second data entry)|(third data entry)"

Not all of the mydata rows are containing the same amount of entries. If mydata contains 0 or just 1 entry, then no "|" pipe symbol is required.

I've tried the following without success:

newdata <- paste(mydata, collapse = "|")

Thanks for your help!

Upvotes: 3

Views: 514

Answers (3)

Anoushiravan R
Anoushiravan R

Reputation: 21938

I think you could use the following solution too:

gsub("(?<=\\))\\s+(?=\\()", "|", mydata, perl = TRUE)

[1] "(first data entry)|(second data entry)|(third data entry) "

Upvotes: 1

micah
micah

Reputation: 1248

This will replace the spaces with the "|" in your string. If you need more complex rules use regex with gsub.

gsub(") (",")|(", yourString)

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627110

You do not need a regex if you have consistent )+1 space+( pattern.

You can simply use

gsub(") (", ")|(", mydata, fixed=TRUE)

If your strings contain variable amount of spaces, tabs, etc., you can use

gsub("\\)\\s*\\(", ")|(", mydata)
gsub("\\)[[:space:]]*\\(", ")|(", mydata)
stringr::str_replace_all(mydata, "\\)\\s*\\(", ")|(")

Here, \)\s*\( pattern matches a ) (escaped because ) is a special regex metacharacter), then zero or more whitespaces, and then a (.

See the regex demo.

If there is always one or more whitespaces between parentheses, use \s+ instead of \s*.

Upvotes: 4

Related Questions