Search code examples
rsplitmultiple-columns

Separate columns into 2


I have a df with a column:

Column A
100_1-A-C-F/G
200_2-B-D-G/F

100_1 is the sample numbers where A/B is the different drug C/D is the different method F/G OR G/F is the different technique

How do I separate the column to look like:

Column A Column B
100_1 A-C-F/G
200_2 B-D-G/F

Thanks

I use separate function and I selected (sep = "_") however, this still includes the sample number:

Column A Column B
100 1-A-C-F/G
200 2-B-D-G/F

Solution

  • You can use separate, but you need to use a regular expression in sep:

    df %>% 
      separate(`Column A`, into = c("Column A", "Column B"), sep="(?<=\\d)-")
    

    Output:

    # A tibble: 2 × 2
      `Column A` `Column B`
      <chr>      <chr>     
    1 100_1      A-C-F/G   
    2 200_2      B-D-G/F 
    

    Input

    df = tibble(
      "Column A" = c("100_1-A-C-F/G", "200_2-B-D-G/F")
    )