Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R separate_longer_delim to split strings with quotation marks

I am trying to use the separate_longer_delim() function from the tidyr package to split a string into separate rows.

This is how the dataset looks like:

Subject Outcome
001 ["Rubbing of nose or ears","Itching of the mouth, tongue, throat","Mild pain","Eye itching"]
002 ["Itchy Tongue", Mild pain","Eye irritation"]

And I would like the dataset to be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Subject Outcome
001 Rubbing of nose or ears
001 Itching of the mouth, tongue, throat
001 Mild pain
002 Itchy Tongue
002 Mild pain
002 Eye irritation

Code for data:

subject <- c('001', '002')
outcome <- c('["Rubbing of nose or ears","Itching of the mouth, tongue, throat","Mild pain","Eye itching"]', '["Itchy Tongue", Mild pain","Eye irritation"]')

dta <- as.data.frame(cbind(subject, outcome))

>Solution :

library(dplyr)
library(stringr)
library(tidyr)

dta |>
  mutate(outcome = str_replace_all(outcome, '\",', "&") |> str_remove_all("[^[\\w,& ]+]")) |>
  separate_longer_delim(outcome, "&")

This replaces ", with a new delimiter & since end quote + a comma delimits the elements of outcome. We can then split on this new delimiter.

Alternatively, you can split on ", and then remove everything that is not a a word character, comma, or space:

dta |>
  separate_longer_delim(outcome, '\",') |>
  mutate(outcome = str_remove_all(outcome, "[^[\\w ,]+]"))

Output

  subject                              outcome
1     001              Rubbing of nose or ears
2     001 Itching of the mouth, tongue, throat
3     001                            Mild pain
4     001                          Eye itching
5     002                         Itchy Tongue
6     002                            Mild pain
7     002                       Eye irritation
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading