So regular expressions are something that I've always struggled a bit with / never spent the due time learning. In this case, I have an R vector of strings with baseball data in this format:
hit_vector = c("", "Batted ball speed <b>104 mph</b>; distance of <b>381
feet</b>; launch angle of <b>38 degrees</b>.",
"Ball was hit at <b>67 mph</b>.", "", "Ball left the bat at <b>107 mph</b> and traveled a distance of <b>412 feet</b>.",
"Batted ball speed <b>71 mph</b>.", "Ball left the bat at <b>94 mph</b> and traveled a distance of <b>287 feet</b>.",
"", "", "Batted ball speed <b>64 mph</b>.")
> hit_vector
[1] ""
[2] "Batted ball speed <b>104 mph</b>; distance of <b>381 feet</b>; launch angle of <b>38 degrees</b>."
[3] "Ball was hit at <b>67 mph</b>."
[4] ""
[5] "Ball left the bat at <b>107 mph</b> and traveled a distance of <b>412 feet</b>."
[6] "Batted ball speed <b>71 mph</b>."
[7] "Ball left the bat at <b>94 mph</b> and traveled a distance of <b>287 feet</b>."
[8] ""
[9] ""
[10] "Batted ball speed <b>64 mph</b>."
I am trying to create a dataframe with 10 rows that looks like this:
hit_dataframe
speed distance degrees
1. NA NA NA
2. 104 381 38
3. 67 NA NA
4. NA NA NA
5. 107 412 NA
6. 71 NA NA
7. 94 287 NA
8. NA NA NA
9. NA NA NA
10. 64 NA NA
The entire hit_vector is much much longer, but it seems that they all follow this naming convention.
Edit: It looks like the following helps to identify some of the info, but these lines aren't working perfectly (the third line returns all FALSE, which isn't right):
grepl("[0-9]{1,3} mph", hit_vector)
grepl("[0-9]{1,3} feet", hit_vector)
grepl("[0-9]{1,3} degrees", hit_vector)
Edit2: I'm not sure how many digits each stat will be. For example mph could be over 100 (3 digits) and also less than 10 (1 digit).