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 8779503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:48:14+00:00 2026-06-13T19:48:14+00:00

I have a data.table with a class column and a number of value columns,

  • 0

I have a data.table with a class column and a number of value columns, e.g.

     class  v1  v2  v3
1:       1  10   3   8
2:       2   2  24   7
3:       1  70   3   9

Now, for a subset of data.table (say class=1), I need to change the order of values in each
row according to a permutation that I have. For instance, if the permutation is

3   1   2

The result should look like

     class  v1  v2  v3
1:       1   8  10   3
2:       2   2  24   7
3:       1   9  70   3

What’s the best way to achieve this using data.table?

I can alternatively convert my data to matrix, if that’s more efficient. Thanks!

  • 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-13T19:48:15+00:00Added an answer on June 13, 2026 at 7:48 pm

    Something like this should work

     DT <- data.table(class = sample(1:3, 10, TRUE), v1 =sample(10), v2 = sample(10), v3 = sample(10))
     DT
        class v1 v2 v3
     1:     1  4  6  6
     2:     1  7  1  5
     3:     1  5  5 10
     4:     1  3  8  7
     5:     3  8  4  3
     6:     3  9  7  9
     7:     2  1  3  8
     8:     2 10 10  2
     9:     1  2  2  4
    10:     2  6  9  1
    
    # the neworder column contains the new permutations
    swapcols <- data.table(class = 1:3, neworder = list(c(1,2,3), c(3,1,2),c(1,3,2)))
    
    setkey(DT, class)
    setkey(swapcols, class)
    
    DT[swapcols, setNames(list(v1,v2,v3)[unlist(neworder)], c('v1','v2','v3'))]
    
        class v1 v2 v3
     1:     1  4  6  6
     2:     1  7  1  5
     3:     1  5  5 10
     4:     1  3  8  7
     5:     1  2  2  4
     6:     2  8  1  3
     7:     2  2 10 10
     8:     2  1  6  9
     9:     3  8  3  4
    10:     3  9  9  7
    

    It would probably be even more efficient to do something like

      DT[swapcols, setcolorder(.SD, unlist(neworder))]
    

    or

      new <- DT[swapcols, list(v1,v2,v3)[unlist(neworder)]]
      setnames(new, names(new), c('class', c('v1','v2','v3'))
    

    You could also use :=. something like

     DT[J(1), `:=`(v1= v2,v2=v3,v3=v1)]
    

    You could try some way of automating this within a function, but it would be a mess of eval / parse and do.call


    From Matthew (tested in v1.8.3) :

    DT = data.table(class=c(1,2,1),v1=c(10,2,70),v2=c(3,24,3),v3=c(8,7,9))
    DT
       class v1 v2 v3
    1:     1 10  3  8
    2:     2  2 24  7
    3:     1 70  3  9
    
    perm = c(3,1,2)
    DT[class==1, names(DT)[-1L] := .SD, .SDcols = perm+1L]]
    DT
       class v1 v2 v3
    1:     1  8 10  3
    2:     2  2 24  7
    3:     1  9 70  3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to find a table with class data in a web page
I have a Data Class called MyTable, and the source property is TABLE (table
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 with many rows and columns. How can I display
Updated I have some huge data, which becomes a large table say table parent
I would like to rename a large number of columns (column headers) to have
I have a table with numerical values as the first column, however it is
In my model I have: class Log < ActiveRecord::Base serialize :data ... def self.recover(table_name,
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have the data table from the jquery plugin dataTables (http://datatables.net/) that I want

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.