Sort a string of comma-separated items alphabetically [duplicate]
Asked Answered
P

1

6

I have a data frame, and one of the columns in the data frame contains a string of items separated by commas like this;

[1] "A, D, B, C"
[2] "D, A, B, C"
[3] "B, A, C, D"
etc...

Is there a way to sort these strings within themselves, so that I can get something like this?:

"A, B, C, D"
"A, B, C, D"
"A, B, C, D"

I am close with the following:

library(gtools)
df$col <- sapply(df$col , function (x)
    mixedsort(strsplit(paste(x, collapse = ','), ',')[[1]]))

But this outputs the results as a list, so I can't do any manipulations in dplyr on the output (like group_by)

Performing answered 16/11, 2017 at 19:35 Comment(0)
N
5
x = c("a, b, c, d", "d, a, b, c", "b, a, c, d")
y = unname(sapply(x, function(x) {
    paste(sort(trimws(strsplit(x[1], ',')[[1]])), collapse=',')} ))
y

[1] "a,b,c,d" "a,b,c,d" "a,b,c,d"

trimws() removes whitespace so sort works correctly on the splitted string. sort() sorts alphabetically. paste(..., collapse = ',') concatenates the sorted vector of strings into a single string.

Nerveracking answered 16/11, 2017 at 20:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.