sorting a list inside of a data.table vector
data.table, r
Solution
This will split the character vectors then sort and return a list for each row
DT[, list(y = lapply(strsplit(x,', '), sort))]
y
1: A11,A12,A41
2: A11,A21,A41,A6,B11
3: A12,A41
4: A12,A41
5: A12,A6,A93,B41
And. if you really wanted the single character strings for each row
DT[, list(y = vapply(strsplit(x,', '), function(x) paste(sort(x), collapse = ', '), FUN.VALUE =character(1)))]
Problem
I have a data table of strings which I am trying to turn into lists and sort without sorting the vectors themselves: ``` > DT <- data.table(x=c("A11, A12, A41", + "A11, A41, B11, A6, A21", + "A41, A12", + "A12, A41", + "A12, A6, B41, A93"));DT x 1: A11, A12, A41 2: A11, A41, B11, A6, A21 3: A41, A12 4: A12, A41 5: A12, A6, B41, A93 ``` which I need to get to: ``` x 1: A11, A12, A41 2: A11, A21, A41, A6, B11 3: A12, A41 4: A12, A41 5: A12, A6, A93, B41 ``` I've tried a using a bunch of `order`ing, `as.lists`, and in `data.frames`, as well as a `data.table` function I tried to write: ``` sortlists <- function(DT,col){ for(i in 1:length(DT[,col])){ DT[i,col]=order(DT[i,col]) } } ``` which throws an error that my column position is greater than ncol(x). Regardless, there must be a better way to get the result I want. I'm fairly new to R and am very new to data.tables (which I would like to impliment more since I have heard good things) so any help would be greatly appreciated!