I have a column in a dataframe called parm_value that I would like to split into two columns, lower and upper bound based on the position of the underscore in the field. I have been trying to use a combination of grep and substring with no success
Current dataframe format:
parm_value
1 30_34
2 60_64
3 65_69
4 75_79
5 90_94
Desired data frame format:
parm_value lower_bound upper_bound
1 30_34 30 34
2 60_64 60 64
3 65_69 65 69
4 75_79 75 79
5 90_94 90 94
I have been trying things like
dat02 <-
dat01 %>%
mutate(lower_bound = substring(parm_value, 1, grep("_", parm_value) - 1)
Use
strsplit
:If it is a really large table you can save runtime by only running the
strsplit
once, then calling the object when defining the newdata.table
fields.strsplit
returns a list:The
sapply
call iterates through the list with the subsetting function[[
selecting the Nth item, where N is given insapply
assapply(some_list, "[[", N)
.