Dina
Dina

Reputation: 31

Split two columns simultaneously in R

I would like to go from this:

    data <- data.frame(sequence = "abc", position = "1;2", probability = "0;1")
        
    > data
          sequence position probability
        1      abc      1;2         0;1

to this:

result <- data.frame(sequence = "abc", position = c(1,2), probability = c(0,1))

    > result
      sequence position probability
    1      abc        1           0
    2      abc        2           1

Do you have an idea of how I could achieve this? I have been playing around with separate and pivot_longer but I feel like there must be a smarter way to achieve the end result.

Thank you for your help!

Upvotes: 1

Views: 39

Answers (3)

akrun
akrun

Reputation: 887881

An option with cSplit

library(data.table)
cSplit(data, c('position', 'probability'), sep=";", "long")
#   sequence position probability
#1:      abc        1           0
#2:      abc        2           1

Upvotes: 0

ThomasIsCoding
ThomasIsCoding

Reputation: 102700

Here is a data.table option

library(data.table)
setDT(data)[, lapply(.SD, function(x) unlist(strsplit(x, ";"))), sequence]

which gives

   sequence position probability
1:      abc        1           0
2:      abc        2           1

Upvotes: 0

ricoderks
ricoderks

Reputation: 1619

You can use separate_rows.

library(tidyverse)

data <- data.frame(sequence = "abc", 
                      position = "1;2", 
                      probability = "0;1")


data %>% 
  separate_rows(c(position, probability), sep = ";")
#> # A tibble: 2 x 3
#>   sequence position probability
#>   <chr>    <chr>    <chr>      
#> 1 abc      1        0          
#> 2 abc      2        1

Created on 2021-04-22 by the reprex package (v2.0.0)

Upvotes: 3

Related Questions