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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:45:43+00:00 2026-05-27T03:45:43+00:00

Here is my data. df <- data.frame(Aa = 1:10, Ab = 11:20,Ca = 41:50,

  • 0

Here is my data.

df <- data.frame(Aa = 1:10, Ab = 11:20,Ca = 41:50, Cb = 51:60, Ba = 21:30, Bb = 31:40,
Da = 51:60, Db = 61:70) # actual data has > 6000 columns
df 
   Aa Ab Ca Cb Ba Bb Da Db
1   1 11 41 51 21 31 51 61
2   2 12 42 52 22 32 52 62
3   3 13 43 53 23 33 53 63
4   4 14 44 54 24 34 54 64
5   5 15 45 55 25 35 55 65
6   6 16 46 56 26 36 56 66
7   7 17 47 57 27 37 57 67
8   8 18 48 58 28 38 58 68
9   9 19 49 59 29 39 59 69
10 10 20 50 60 30 40 60 70

mf <- data.frame (SN = 1:5, colname = c("A", "B", "C", "D", "E"))
mf 
  SN colname
1  1       A
2  2       B
3  3       C
4  4       D
5  5       E

I want to order the columns of df (actually pairs with suffix a and b) based on mf$colname. Exclude those column that is not in mf.

So output will look like:

df1 <- data.frame(Aa = 1:10, Ab = 11:20, Ba = 21:30, Bb = 31:40,Ca = 41:50, Cb = 51:60,
    Da = 51:60, Db = 61:70)   
df1
   Aa Ab Ba Bb Ca Cb Da Db
1   1 11 21 31 41 51 51 61
2   2 12 22 32 42 52 52 62
3   3 13 23 33 43 53 53 63
4   4 14 24 34 44 54 54 64
5   5 15 25 35 45 55 55 65
6   6 16 26 36 46 56 56 66
7   7 17 27 37 47 57 57 67
8   8 18 28 38 48 58 58 68
9   9 19 29 39 49 59 59 69
10 10 20 30 40 50 60 60 70

Edits:

order does not mean alphabetic order. order based on mf$colnames

thanks;

Major edits:
I think the example I provided is clear what I want to do, as I my desired column by coincidence alphabetical order. But my intention is not true. Please consider another example.

 df <- data.frame (k1a = 1:10, k1b = 11:20, a1a = 21:30, a1b = 31:40, 
j1a = 41:50, j1b = 51:60, p1a = 61:70, p1b = 71:80)
df
   k1a k1b a1a a1b j1a j1b p1a p1b
1    1  11  21  31  41  51  61  71
2    2  12  22  32  42  52  62  72
3    3  13  23  33  43  53  63  73
4    4  14  24  34  44  54  64  74
5    5  15  25  35  45  55  65  75
6    6  16  26  36  46  56  66  76
7    7  17  27  37  47  57  67  77
8    8  18  28  38  48  58  68  78
9    9  19  29  39  49  59  69  79
10  10  20  30  40  50  60  70  80

mf <- data.frame (SN = 1:5, colname = c("p1", "a1", "j1", "k1", "e1")
mf 
  SN colname
1  1      p1
2  2      a1
3  3      j1
4  4      k1
5  5      e1

this desired output

df1 <- data.frame ( p1a = 61:70, p1b = 71:80,  a1a = 21:30, a1b = 31:40,
  j1a = 41:50, j1b = 51:60, k1a = 1:10, k1b = 11:20)
df1
   p1a p1b a1a a1b j1a j1b k1a k1b
1   61  71  21  31  41  51   1  11
2   62  72  22  32  42  52   2  12
3   63  73  23  33  43  53   3  13
4   64  74  24  34  44  54   4  14
5   65  75  25  35  45  55   5  15
6   66  76  26  36  46  56   6  16
7   67  77  27  37  47  57   7  17
8   68  78  28  38  48  58   8  18
9   69  79  29  39  49  59   9  19
10  70  80  30  40  50  60  10  20
  • 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-27T03:45:43+00:00Added an answer on May 27, 2026 at 3:45 am

    Changed answer:

    To the updated question, I think you can do it as followed. Given dataset:

    df <- data.frame (k1a = 1:10, k1b = 11:20, a1a = 21:30, a1b = 31:40, 
    j1a = 41:50, j1b = 51:60, p1a = 61:70, p1b = 71:80)
    
    mf <- data.frame (SN = 1:5, colname = c("p1", "a1", "j1", "k1", "e1"))
    

    You can use grep() to find the indexes of variables names that correspond to each of the elements of mf$colnane

    df1 <- df[unlist(sapply(as.character(mf$colname),grep,names(df)))]
    

    This becomes:

    > df1 
       p1a p1b a1a a1b j1a j1b k1a k1b
    1   61  71  21  31  41  51   1  11
    2   62  72  22  32  42  52   2  12
    3   63  73  23  33  43  53   3  13
    4   64  74  24  34  44  54   4  14
    5   65  75  25  35  45  55   5  15
    6   66  76  26  36  46  56   6  16
    7   67  77  27  37  47  57   7  17
    8   68  78  28  38  48  58   8  18
    9   69  79  29  39  49  59   9  19
    10  70  80  30  40  50  60  10  20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQLDF newbie here. I have a data frame which has about 15,000 rows and
Here is some example data: data = data.frame(series = c(1a, 1b, 1e), reading =
ex: <a><strike>example data in here</strike></a> I want everything inside the a tag, to the
I have a data.frame , called so_data. Columns 13:23 are list s, which hold
I'm using reshape in R to compute aggregate statistics over columns of a data.frame.
I want make changes in big data.frame column names (names(mp)) with the help of
I have a data.frame in R which has been constructed off the Example 1-3
I see that when Hadley Wickam prints data frame here , the values which
Here's the code I use to find numeric variables in a data frame: Data
Here is an example data set: data <- data.frame (author = c('bob', 'john', 'james'),

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.