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

  • Home
  • SEARCH
  • 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 8219219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:04:46+00:00 2026-06-07T13:04:46+00:00

This is one of those there has to be a function for this questions.

  • 0

This is one of those “there has to be a function for this” questions. It’s not that big a deal, but it’s just annoying enough that every time I rename an object I wonder if there’s a better way.

Suppose I capitalize an object that I’ve created and realize I’d rather have it uncapitalized:

# Create test data
X <- runif(100)
# Rename the object
x <- X
rm(X)

Is there a one-command way of doing this (that also avoids the re-copy for memory/speed reasons)? There are a few commands named rename in various packages but they all work on elements within a list, rather than on the list (or other object) itself.

  • 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-07T13:04:49+00:00Added an answer on June 7, 2026 at 1:04 pm

    I don’t know of a built in way to do this but you could easily write your own function to do something along these lines. For instance this does just that without any checking to make sure the object exists or whether or not there is already an object named what you want to rename to.

    mv <- function(x, y){
        x_name <- deparse(substitute(x))
        y_name <- deparse(substitute(y))
    
        assign(y_name, x, pos = 1)
        rm(list = x_name, pos = 1)
        invisible()
    }
    

    Some example use

    > x <- 3
    > x
    [1] 3
    > y
    Error: object 'y' not found
    > mv(x, y)
    > x
    Error: object 'x' not found
    > y
    [1] 3
    

    Edit: For those that didn’t follow the link in the comments here is a version written by Rolf Turner that does some checking to make sure the object we want to move actually exists and asks us if we want to overwrite an existing object if the new name already has an object in it.

    mv <- function (a, b) {
        anm <- deparse(substitute(a))
        bnm <- deparse(substitute(b))
        if (!exists(anm,where=1,inherits=FALSE))
            stop(paste(anm, "does not exist.\n"))
        if (exists(bnm,where=1,inherits=FALSE)) {
            ans <- readline(paste("Overwrite ", bnm, "? (y/n) ", sep = ""))
            if (ans != "y")
                return(invisible())
        }
        assign(bnm, a, pos = 1)
        rm(list = anm, pos = 1)
        invisible()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is one of those I probably should know this, but I don't questions.
Hey, so this is one of those questions that seems obvious, and I'm probably
There is one thing that bothers me, C++ function pointers. I'm asking this question
This is one of those questions that seems to fall into the naively obvious
This is one of those things that you read once, say aha! and then
This is just one of those little things I did all the time in
Hey this is going to be one of those dumb questions. I am trying
I think this is going to be one of those simple-when-you-see-it problems, but it
There's a truly a lot of questions that seem similar to this on SO,
I've posted a few questions prior to this one regarding the use of std::function

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.