I have a vector data (This is a column in a data frame):
[1] "Tue 12-14 (w1-6, CLB 6)" "Mon 18-20 (w1-6, ColomboThC)" "Thu 14-16 (w1-6,7-9,10-12, CLB 8)"
[4] "Fri 13 (w2-9,10-13, Law 388)" "Fri 14 (w2-9,10-13, Sqhouse206)" "Fri 15 (w2-9,10-13, Sqhouse115)"
[7] "Thu 17 (w2-9,10-13, Block G16)" "Thu 18 (w2-9,10-13, Block G16)" "Mon 10 (w2-9,10-13, AinswthG01)"
[10] "Mon 11 (w2-9,10-13, Sqhouse203)" "Mon 12 (w2-9,10-13, Sqhouse206)" "Mon 13 (w2-9,10-13, BUS 114)"
[13] "Mon 16 (w2-9,10-13, Gold G03)" "Mon 17 (w2-9,10-13, Quad G047)" "Mon 20 (w2-9,10-13, Col LG02)"
[16] "Tue 17 (w2-9,10-13, Quad 1001)" "Tue 18 (w2-9,10-13, Quad 1001)" "Tue 19 (w2-9,10-13, Quad 1001)"
[19] "Tue 20 (w2-9,10-13)" "Wed 10 (w2-9,10-13, Quad 1046)" "Wed 11 (w2-9,10-13, Quad 1046)"
[22] "Wed 12 (w2-9,10-13, Quad 1046)" "Wed 13 (w2-9,10-13, Quad G046)"
I would like to extract strings based on the patterns, so the expected output, as an example, for the first element of the vector would be:
"Tue" "12-14" "1-6" "CLB 6"
Example of output of the third element would be:
"Thu" "14-16" c("1-6","7-9","10-12") "CLB 8"
where c("1-6","7-9","10-12") is a list.
(Note that each of this will be appended as new columns in my data frame.)
I'm thinking of using gsub to extract each part of the string. Is there other functions I could use?
Any advice is much appreciated :)
?strsplitis often helpful for this sort of thing too. Specify what splits your segments of text. " (w" or ")" or ", " it looks like. Put each of those into astrsplittogether likestrsplit(x, "\\s+\\(w|\\)|,\\s+")and you will be half way there.