I am working with baseball data in R and trying to assign a weight (avg_re) to certain situations, namely how many outs there are and how many runners are on base. I have two data sets, re_table and Season_RE.
re_table has two columns, the situation (base_out_state) and the weight (avg_re). The first row is basically telling us that when there is someone on 1b, 2b, and 3b with 0 outs, 2.57 runs are expected to be scored. Season_RE takes the situation column and divides it into four new columns (outs, 1b, 2b, and 3b).
This is what re_table looks like:

This is what Season_RE looks like:

What I am trying to do is separate base_out_state from re_table into four new categories. I have successfully done it for the outs but I don't know how to do it for 1b, 2b, or 3b. The code I found online used this to do it but as you can see it hasn't done anything for me.
library(stringr)
for (i in 1:nrow(re_table)){
Season_RE$outs_when_up[i] = str_split(
re_table$base_out_state, " ")[[i]][[1]]
Season_RE$on_1b[i] = ifelse(str_split(
re_table$base_out_state, "")[[i]][[5]] == "1b", 1, 0)
Season_RE$on_2b[i] = ifelse(str_split(
re_table$base_out_state, " ")[[i]][[6]] == "2b", 1, 0)
Season_RE$on_3b[i] = ifelse(str_split(
re_table$base_out_state, " ")[[i]][[7]] == "3b", 1, 0)
}
I have never used stringr before and have no idea what I am doing. If you want to take a look at the whole code, here is the link: https://github.com/ethanmoore21/PitchQuality/blob/master/qop_3_rfs.R