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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:25:49+00:00 2026-05-23T05:25:49+00:00

I recently stumbled across replace() and [<- . They seem to have similar functionality,

  • 0

I recently stumbled across replace() and "[<-". They seem to have similar functionality, for example with "[<-" I can do something like this:

        > x.tst <- array(1:6, c(2,3))
        > s.tst <- array(0, c(2,3))
        > s.tst
             [,1] [,2] [,3]
        [1,]    0    0    0
        [2,]    0    0    0
        > s.tst[1:3] <- 1
        > "[<-"(x.tst, s.tst==1, 0)
             [,1] [,2] [,3]
        [1,]    0    0    5
        [2,]    0    4    6
        > x.tst
             [,1] [,2] [,3]
        [1,]    1    3    5
        [2,]    2    4    6

Can somebody help to clarify the difference? What are the strengths of replace vs "[<-" and vis versa?

  • 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-23T05:25:50+00:00Added an answer on May 23, 2026 at 5:25 am

    They’re basically exactly the same thing. If you look at the source code of replace, you’ll see :

    function (x, list, values) 
    {
        x[list] <- values
        x
    }
    <environment: namespace:base>
    

    So replace is nothing else but a wrapper around [<- :

    > replace(x.tst, s.tst==1, 0)
         [,1] [,2] [,3]
    [1,]    0    0    5
    [2,]    0    4    6
    

    Using [<- can give you a speedup if you need to do this a million times, as you lose the extra call to the wrapper function. But it’s really marginal, so it’s a matter of choice. I would say that replace()is a bit more readible

    Btw, x.tst[s.tst==1] <- 0 is quite more readible than "[<-"(x.tst, s.tst==1, 0) . No reason to use that construct, unless you want to save the result in a new dataframe.

    To clarify, as @Andrie pointed out, both with replace() and "[<-"(x.tst, s.tst==1, 0) you get a copy of the whole x.tst with the relevant values changed. So you can put that in a new object. This is contrary to x.tst[s.tst==1] <- 0, where you change the values in x.tst itself. Mind you, it doesn’t save on memory, as R will make internally a copy of x.tst before doing the manipulation.

    Timing results :

    > system.time(replicate(1e6, replace(x.tst, s.tst==1, 0)))
       user  system elapsed 
      12.73    0.03   12.78 
    
    > system.time(replicate(1e6, "[<-"(x.tst, s.tst==1, 0)))
       user  system elapsed 
       6.42    0.02    6.44 
    
    > system.time(replicate(1e6, x.tst[s.tst==1] <- 0))
       user  system elapsed 
       5.28    0.02    5.32 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I stumbled across this pretty slick JS library called nodeJS that acts like
I recently stumbled across this entry in the google testing blog about guidelines for
I have recently stumbled across an issue where the WPF ListView control seems to
I've been fiddling around with arrays and array ordering recently and stumbled across something
Recently I stumbled across mongoDB, couchDB etc. I am hoping to have a play
I'm refactoring some code that a friend wrote and recently stumbled across this function:
I have recently stumbled upon an open source project that I would like to
I'm new to this site having stumbled across it recently (and am loving it!)
I recently stumbled across an article that claims Microsoft is banning the memcpy() function
I have recently stumbled upon a problem with selecting relationship details from a 1

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.