r - adding a comma and "" to a specific text -


i don't know category question falls into. have text has pattern below.

1 merrill lynch 33 2 lehman brothers hldgs. 82 3 salomon 149 4 paine webber group 248 5 bear stearns 328 6 charles schwab 621 7 a.g. edwards & sons 823

the pattern (sequence 1, company name (consists of characters or numbers), number (maximum 1000)) repeated

i want (build function) turns text vector;

c("1 merrill lynch 33", "2 lehman brothers hldgs. 82", "3 salomon 149",    "4 paine webber group 248", "5 bear stearns 328", "6 charles schwab 621",    "7 a.g. edwards & sons 823") 

would possible? there's no regularity in company name or number follows. there's space after first increasing sequence, space after company name. can provide more information if necessary.

using stringr package,

library(stringr) str_extract_all(txt, "[0-9]+\\d+[0-9]+") 

the regular expression reads 'any number of digits', 'anything except digits', 'any number of digits'.

gives

[[1]] [1] "1 merrill lynch 33"          "2 lehman brothers hldgs. 82" "3 salomon 149"               [4] "4 paine webber group 248"    "5 bear stearns 328"          "6 charles schwab 621"        [7] "7 a.g. edwards & sons 823" 

note result list.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -