Two delimited data in R

I have a text file with more than one separator. This is sample data:

12 ->3 4 5
14->2 1
1->3 5 6

I wonder if there is an easy way to get the data in the following format:

12 3
12 4
12 5
14 2
14 1
 1 3
 1 5
 1 6
+4
source share
2 answers

I tried to reproduce your situation using cat, and I hope that this is what you actually have. 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 quickly read it, specifying some kind of invalid separator, so the result will be one set of column data

library(data.table)
dt <- fread("test.txt", 
            sep = ",", 
            header = FALSE)

The next step is a double split, first dividing the numbers on both sides of the arrow ( ->), and then dividing into a 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
+8
source

textConnection :

 txt <- "12 ->3 4 5
 14->2 1
 1->3 5 6"

 inpt <- strsplit(readLines(textConnection(txt)), "\\s*->")
 inpt
#---------
[[1]]
[1] "12"    "3 4 5"

[[2]]
[1] "14"  "2 1"

[[3]]
[1] "1"     "3 5 6"

do.call(rbind.data.frame, 
     lapply(inpt, function(i) {inp <- scan(text=i[2])
                               list( col1= rep(i[1], length(inp) ), col2=inp)} ) )

#  --- can suppress the scan read messages ... see `?scan`
Read 3 items
Read 2 items
Read 3 items
   col1 col2
4    12    3
5    12    4
6    12    5
41   14    2
51   14    1
61    1    3
7     1    5
8     1    6
+4

Source: https://habr.com/ru/post/1625230/


All Articles