Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8947655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:49:13+00:00 2026-06-15T12:49:13+00:00

I have a data table of strings which I am trying to turn into

  • 0

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 ordering, 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:49:15+00:00Added an answer on June 15, 2026 at 12:49 pm

    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)))]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file containing seed data that I'm trying to load into
I have an excel file which I am trying to put into mysql. There
I have a table of data which I would like to order by the
I have a local database table that contains 50+ external data sources from which
I have a question with respect to putting JSON data into a table view.
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have a data table with many rows and columns. How can I display
I have the data table from the jquery plugin dataTables (http://datatables.net/) that I want
I am trying to export some data from a SQL Express table to Access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.