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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:24:55+00:00 2026-05-31T18:24:55+00:00

I am working on a large dataframe with >40 columns. I want to be

  • 0

I am working on a large dataframe with >40 columns. I want to be able to move a column, without having to specify all the column names. For example:

a<-c(1:5)  
b<-c(4,3,2,1,1)  
Percent<-c(40,30,20,10,10)
Labels<-c("Cat","Dog","Rabbit","Rat","Mouse")
df1<-data.frame(a,b,Percent,Labels)

How would I move the column ‘Lables’ to before column ‘a’ WITHOUT having to write all the other column names (i.e. can I just specify a column to come before/after another column?).

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-05-31T18:24:56+00:00Added an answer on May 31, 2026 at 6:24 pm

    Something quick and dirty would be (i.e. no sanity checking etc. and assuming only a single colname is supplied):

    moveToFirstCol <- function(df, colname) {
        cnams <- colnames(df)
        want <- which(colname == cnams)
        df[, c(cnams[want], cnams[-want])]
    }
    

    which gives:

    > moveToFirstCol(df1, "Labels")
      Labels a b Percent
    1    Cat 1 4      40
    2    Dog 2 3      30
    3 Rabbit 3 2      20
    4    Rat 4 1      10
    5  Mouse 5 1      10
    

    That should suggest a way to handle this sort of thing if you need additional flexibility.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a large C++ solution in Visual Studio 2005. I want
I'm working on a large Flash CS4 project with multiple swfs and want to
We are working with large amounts of data, all tagged in UTC (in Java).
I'm a freelancer and prefer working on large projects all by my own self.
Im working with a large dataset of point of interest (POI) which all have
Combining 2 columns into 1 column many times in a very large dataset in
I am working on a large dataset, an example of which is shown below:
Background: I am writing a C++ program working with large amounts of geodata, and
I often jump from a file to file, while working a large codebase. I
When working with very large documents, would it basically overwhelm the connection and ground

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.