I have a text file that has more than one delimiter. This a sample of the data:
12 ->3 4 5
14->2 1
1->3 5 6
I wonder if there is a simple way to obtain the data in the following format:
12 3
12 4
12 5
14 2
14 1
1 3
1 5
1 6
I was trying to reproduce your situation using cat
and hope it is what you really have there. So let's say this is your file
cat("12 ->3 4 5
14->2 1
1->3 5 6",
file = "test.txt")
Using data.table
, I'm reading it quickly by specifying some wrong separator so the result will be a single column data set
library(data.table)
dt <- fread("test.txt",
sep = ",",
header = FALSE)
Next step is a double split, first separating the numbers on both sides of the arrow (->
), and then splitting by group
dt[, tstrsplit(V1, "\\s*->\\s*", type.convert = TRUE)
][, strsplit(V2, "\\s+"), by = .(indx = V1)]
# indx V1
# 1: 12 3
# 2: 12 4
# 3: 12 5
# 4: 14 2
# 5: 14 1
# 6: 1 3
# 7: 1 5
# 8: 1 6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With