1

I have a dataframe like the following:

ref = c("ab/1bc/1", "dd/1", "cc/1", "2323")
text = c("car", "train", "mouse", "house")

data = data.frame(ref, text)

Which produces this:

enter image description here

IF the cell within the ref column has /1 in it, I want to split it and duplicate the row.

I.e. the table above should look like this:

enter image description here

I have the following code, which splits the cell by the /1, but it also removes it. I thought about adding /1 back onto every ref, but not all refs have it.

data1 = data %>%
    mutate(ref = strsplit(as.character(ref), "/1")) %>%
    unnest(ref)

Some of the other answers use regex for when people split by things like &/,. etc, but not /1. Any ideas?

Nicholas
  • 3,517
  • 13
  • 47
  • 86

1 Answers1

1

With separate_rows and look-behind:

library(tidyr)
library(dplyr)
data %>%
  separate_rows(ref, sep = "(?<=/1)") %>% 
  filter(ref != "")

output

# A tibble: 5 × 2
  ref   text 
  <chr> <chr>
1 ab/1  car  
2 bc/1  car  
3 dd/1  train
4 cc/1  mouse
5 2323  house

Or with strsplit:

data %>%
  mutate(ref = strsplit(ref, "(?<=/1)", perl = TRUE)) %>%
  unnest(ref)
Maël
  • 45,206
  • 3
  • 29
  • 67
  • 1
    Thank you so much Maël!!!... this works PERFECTLY and saves me so much time! I need to work on my regex. – Nicholas Dec 12 '22 at 12:53