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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:22:56+00:00 2026-06-10T13:22:56+00:00

Slightly embarrassed to ask such a simple question but I’ve wasted an hour now

  • 0

Slightly embarrassed to ask such a simple question but I’ve wasted an hour now and figure its a 30 second solution. The problem is how to edit an existing object that is provided as an input to a function. I’ve also played with the super-assignment <<- without success.

The example function uses 2 inputs (one for an object and one for its name). I just need a form of this that removes the need for the ‘n’ input.

m <- c(2,5,3,7,1,3,9,3,5)
dim(m) <- c(3,3)
m

f <- function(x, n) { # where 'n' is the object name of 'x'
  x[1,] <- c(1,2,3)
  assign(n, x, envir = .GlobalEnv)
}

f(m, 'm')
m

Thanks in advance.

  • 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-10T13:22:58+00:00Added an answer on June 10, 2026 at 1:22 pm

    You don’t need to provide the name as an extra argument; substitute will get that for you. To do things in the scope of the calling function you use eval with parent.frame.

    f <- function(x) {
      eval(substitute( x[1,] <- c(1,2,3) ), parent.frame())
    }
    

    Then,

    m <- c(2,5,3,7,1,3,9,3,5)
    > dim(m) <- c(3,3)
    > m
         [,1] [,2] [,3]
    [1,]    2    7    9
    [2,]    5    1    3
    [3,]    3    3    5
    > f(m)
    > m
         [,1] [,2] [,3]
    [1,]    1    2    3
    [2,]    5    1    3
    [3,]    3    3    5
    

    That said, modifying the caller’s environment is generally a bad idea and it will usually lead to less confusing/fragile code if you just return the value and re-assign it to m instead. This is generally preferable.:

    f <- function (x) {
        x[1,] <- c(1,2,3)
        x
    }
    
    m <- f(m)
    

    However, I have occasionally found eval shenanigans to come in handy when I really needed to change an array in place and avoid an array copy.

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

Sidebar

Related Questions

This is probably a simple question, and I'm slightly embarrassed to ask it, but
Another slightly non-technical question, but I couldn't decide whether to ask here or on
A slightly archaic question I'm afraid but here goes: I have a program which
This is a slightly tricky question. I am using NSDateFormatter on the iPhone but
Slightly related to this question , but you don't really need to read that.
Slightly strange question, but hopefully someone can help. In essence, if the time was
Slightly oxymoronic title! Bonus points for Xcode answer but it's a gdb question. If
Slightly off-topic question, but I was wondering if anybody could tell me when and
Slightly broad question here, but here goes I'm trying to call an Oracle stored
A slightly different requirement to this question The dictionary stays the same format but

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.