I am quite a beginner in R, and I faced this problem.
I would like to find the length of the shortest string per each category in my tibble and then, truncate all strings from the category according to width of the shortest one.
ex = tibble( category = c("A", "A", "C", "B", "C", "A"),
string = c("cat", "bird", "apple", "cloud", "banana", "elephant"))
I presume how to solve the problem theoretically, however, I am not able to put it together.
ex %>%
group_by(category) %>%
mutate(length = lapply(ex, function(x) min(nchar(x))) ) %>%
somehow str_trunc() ?
At the end I would like to see something like that:
ex = tibble( category = c("A", "A", "C", "B", "C", "A"),
string = c("cat", "bir", "apple", "cloud", "banan", "ele"))