Note that if you run your command outside of mutate -- splitdesc(test2$DESC) -- you'll get the same error. This is an indication that the problem is due to the changing input, not because it's in mutate.
Let's try debugging the function. One way is to run the code outside of the function
t <- test2$DESC
t1 <- str_sub(t, 1, 25)
x1 <- str_locate_all(t1, " ") # get positions of all spaces
x1df <- as.data.frame(x1) # convert to data frame to find nrow
# Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
# arguments imply differing number of rows: 3, 4
What does x1 look like at that point?
[[1]]
start end
[1,] 6 6
[2,] 14 14
[3,] 22 22
[[2]]
start end
[1,] 4 4
[2,] 10 10
[3,] 16 16
[4,] 20 20
So, str_locate_all produces a list. Since you want to get the last row value from each item in the list, purrr::map_int is a good step:
s1 <- map_int(x1, ~ .x[nrow(.x), 1])
That will get you the vector that you're after.
On the other hand, I tend to like regexes, so I would likely replace splitdesc with something like this:
splitdesc2 <- function(inp) {
str_extract(inp, "^.{1,25} ") %>%
str_trim()
}
The regex "^.{1,25} " finds the longest string 25 characters or less that is followed by a space.