I am posting a slightly modified version of the code just for future reference. In the actual data set, the last words do not have the length. If you can think of a better solution, do post!
library(tidyverse)
df <- tibble(
v1 = c(
"average of blah",
"average of clah",
"Dlah"
)
)
df %>%
mutate(
v2 = if_else(stringr::str_detect(v1, "average of"),
stringr::str_sub(v1, 12), v1),
v2 = stringr::str_to_title(v2)
)
#> # A tibble: 3 x 2
#> v1 v2
#> <chr> <chr>
#> 1 average of blah Blah
#> 2 average of clah Clah
#> 3 Dlah Dlah