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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:23:34+00:00 2026-05-30T08:23:34+00:00

I have a data frame where columns are constantly being added to it. I

  • 0

I have a data frame where columns are constantly being added to it. I also have a total column that I would like to stay at the end. I think I must have skipped over some really basic command somewhere but cannot seem to find the answer anywhere. Anyway, here is some sample data:

x=1:10
y=21:30
z=data.frame(x,y)
z$total=z$x+z$y
z$w=11:20
z$total=z$x+z$y+z$w

When I type z I get this:

    x  y total  w
1   1 21    33 11
2   2 22    36 12
3   3 23    39 13
4   4 24    42 14
5   5 25    45 15
6   6 26    48 16
7   7 27    51 17
8   8 28    54 18
9   9 29    57 19
10 10 30    60 20

Note how the total column comes before the w, and obviously any subsequent columns. Is there a way I can force it to be the last column? I am guessing that I would have to use ncol(z) somehow. Or maybe not.

  • 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-30T08:23:36+00:00Added an answer on May 30, 2026 at 8:23 am

    You can reorder your columns as follows:

    z <- z[,c('x','y','w','total')]
    

    To do this programmatically, after you’re done adding your columns, you can retrieve their names like so:

    nms <- colnames(z)
    

    Then you can grab the ones that aren’t ‘total’ like so:

    nms[nms!='total']
    

    Combined with the above:

    z <- z[, c(nms[nms!='total'],'total')]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame, d1, that has 7 columns, the 5th through 7th column
I have a data frame with two columns. First column contains categories such as
I have a dataframe with a column of integers that I would like to
Suppose that you have a data frame with many rows and many columns. The
I have a data.frame that looks like this > head(df) Memory Memory Memory Memory
I have a data frame that looks as follows (8 columns - the myPOSIX
If I have a data frame where I am adding columns, and I would
I have a data frame with columns that, when concatenated (row-wise) as a string,
I have a data frame with 22239 rows & 200 columns. The first column
Suppose, you have a data.frame like this: x <- data.frame(v1=1:20,v2=1:20,v3=1:20,v4=letters[1:20]) How would you select

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.