Наиболее очевидный подход: разбить строку на запятую, sort
вставить ее, а затем вставить обратно.
df$Themes <- sapply(strsplit(df$Themes, ","), function(x)
paste0(sort(x), collapse = ","))
df
# Themes
#1 eggs,fruit,toast
#2 chocolate,eggs,milk
#3 apple,fruit,milk
Используя ту же логику с tidyverse
library(tidyverse)
df %>%
rowwise() %>%
mutate(Themes = str_c(str_sort(str_split(Themes, ",",simplify = TRUE)),
collapse = ","))
# Themes
# <chr>
#1 eggs,fruit,toast
#2 chocolate,eggs,milk
#3 apple,fruit,milk
данные
df <- structure(list(Themes = c("toast,eggs,fruit", "chocolate,eggs,milk",
"fruit,apple,milk")), .Names = "Themes", row.names = c(NA, -3L
), class = "data.frame")