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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:34:38+00:00 2026-05-27T09:34:38+00:00

Possible Duplicate: How to sort a dataframe by column(s) in R Here is the

  • 0

Possible Duplicate:
How to sort a dataframe by column(s) in R

Here is the dataset:

odervect <- c("xaf", "c3a", "c3b", "ka101","jk12", "cd101", "kl01v", "klm1")
odervect
[1] "xaf" "c3a"   "c3b"   "ka101" "jk12"  "cd101" "kl01v" "klm1"  

dfd <- data.frame(ka101 = 101:110,c3a = 1:10, kl01v = 301:310, xaf = 11:20,
    c3b = 41:50, cd101 = 61:70)


 dfd 

   ka101 c3a kl01v xaf c3b cd101
1    101   1   301  11  41    61
2    102   2   302  12  42    62
3    103   3   303  13  43    63
4    104   4   304  14  44    64
5    105   5   305  15  45    65
6    106   6   306  16  46    66
7    107   7   307  17  47    67
8    108   8   308  18  48    68
9    109   9   309  19  49    69
10   110  10   310  20  50    70

Desired output:

 dfd1 <- data.frame(xaf = 11:20, c3a = 1:10, c3b = 41:50,ka101 = 101:110, cd101 = 61:70,
     ka101v = 301:310)
    dfd1 
        xaf c3a c3b ka101 cd101 ka101v
1   11   1  41   101    61    301
2   12   2  42   102    62    302
3   13   3  43   103    63    303
4   14   4  44   104    64    304
5   15   5  45   105    65    305
6   16   6  46   106    66    306
7   17   7  47   107    67    307
8   18   8  48   108    68    308
9   19   9  49   109    69    309
10  20  10  50   110    70    310

Please note that not all names in order vector are present in dfd, however only need to sort the matching ones.

  • 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-05-27T09:34:38+00:00Added an answer on May 27, 2026 at 9:34 am

    How about this? It first uses %in% to select (still in order) the strings in odervect that correspond to column names present in dfd:

    cnames <- odervect[odervect %in% names(dfd)]
    dfd[cnames]
       xaf c3a c3b ka101 cd101 kl01v
    1   11   1  41   101    61   301
    2   12   2  42   102    62   302
    3   13   3  43   103    63   303
    4   14   4  44   104    64   304
    5   15   5  45   105    65   305
    6   16   6  46   106    66   306
    7   17   7  47   107    67   307
    8   18   8  48   108    68   308
    9   19   9  49   109    69   309
    10  20  10  50   110    70   310
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to sort a Map<Key, Value> on the values in Java? Here's
Possible Duplicate: How to sort an NSMutableArray with custom objects in it? Here i
Possible Duplicate: Sort multidimensional array by multiple keys I am trying to find some
Possible Duplicate: Sort by key of dictionary inside a dictionary in Python I have
Possible Duplicate: DataGridView Sort Programmitically How Sort Datagridview ?
Possible Duplicate: What is a bubble sort good for? I'am sure every algorithm has
Possible Duplicate: C++ struct sorting Is it possible to sort a vector in C++
Possible Duplicate: Python analog of natsort function (sort a list using a “natural order”
Possible Duplicate: Which sorting algorithm is used by STL’s list::sort()? Which sorting algorithm can
Possible Duplicate: Sort a matrix with another matrix The Matrix A ('10 x 1000'

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.