2

I have the following string I need to extract the patterns into a single column data frame named SIZE

str <- "N · 0.1 [mm]: N · 0.1 + 0.02 [mm]: N · 0.1 + 0.05 [mm] N · 0.1 + 0.08 [mm] M · 1 [mm]: M · 1 + 0.5 [mm] M · 1 + 0.75 [mm]"

The patterns are either followed by : or whitespace and always ends in [mm]

The regex I am using to match my patterns is and it works, but i'm not sure how to extract the matches to create a column as a data frame.

\S\W+\d\.?\d?\s\+?\s?\d?\.?\d?\d?\s?\[mm\]

Output expected: 1 column named SIZE

       N · 0.1 [mm]
N · 0.1 + 0.02 [mm]
N · 0.1 + 0.05 [mm]
N · 0.1 + 0.08 [mm]
         M · 1 [mm]
   M · 1 + 0.5 [mm]
  M · 1 + 0.75 [mm]

Any help appreciated. Thanks..

0

2 Answers 2

5

Perhaps, strsplit would make things easier here..

str  <- "N · 0.1 [mm]: N · 0.1 + 0.02 [mm]: N · 0.1 + 0.05 [mm] N · 0.1 + 0.08 [mm] M · 1 [mm]: M · 1 + 0.5 [mm] M · 1 + 0.75 [mm]"
vals <- strsplit(str, '(?<=\\])[\\s:]*', perl = T)
data.frame(SIZE = unlist(vals))

Output

                 SIZE
1        N · 0.1 [mm]
2 N · 0.1 + 0.02 [mm]
3 N · 0.1 + 0.05 [mm]
4 N · 0.1 + 0.08 [mm]
5          M · 1 [mm]
6    M · 1 + 0.5 [mm]
7   M · 1 + 0.75 [mm]
Sign up to request clarification or add additional context in comments.

1 Comment

Because of the relationship between data.frames and lists in R, data.frame(setNames(strsplit(str, '(?<=\\])[\\s:]*', perl = T), "SIZE")) would also work. +1
2

Here's one approach to get the data in: replace any instances of "[mm] " with "[mm]: " and scan the text in with ":" as your separator. No fussing with regexes....

scan(what = "", text = gsub("[mm] ", "[mm]: ", str, fixed=TRUE), 
     sep = ":", strip.white=TRUE)
# Read 7 items
# [1] "N · 0.1 [mm]"        "N · 0.1 + 0.02 [mm]" "N · 0.1 + 0.05 [mm]"
# [4] "N · 0.1 + 0.08 [mm]" "M · 1 [mm]"          "M · 1 + 0.5 [mm]"   
# [7] "M · 1 + 0.75 [mm]"  

Just assign the result there to a column in a data.frame or create a data.frame with the output. Or, all in one:

data.frame(
  SIZE = scan(text = gsub("[mm] ", "[mm]: ", str, fixed=TRUE), 
              sep = ":", strip.white=TRUE, what = ""))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.