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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:36:47+00:00 2026-06-07T06:36:47+00:00

We want to set all values in an array zero that are negative. I

  • 0

We want to set all values in an array zero that are negative.

I tried out a a lot of stuff but did not yet achieve a working solution.
I thought about a for loop with condition, however this seems not to work.

#pred_precipitation is our array
pred_precipitation <-rnorm(25,2,4)     

for (i in nrow(pred_precipitation))
{
  if (pred_precipitation[i]<0) {pred_precipitation[i] = 0}
  else{pred_precipitation[i] = pred_precipitation[i]}
}
  • 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-07T06:36:50+00:00Added an answer on June 7, 2026 at 6:36 am

    Thanks for the reproducible example. This is pretty basic R stuff. You can assign to selected elements of a vector (note an array has dimensions, and what you’ve given is a vector not an array):

    > pred_precipitation[pred_precipitation<0] <- 0
    > pred_precipitation
     [1] 1.2091281 0.0000000 7.7665555 0.0000000 0.0000000 0.0000000 0.5151504 0.0000000 1.8281251
    [10] 0.5098688 2.8370263 0.4895606 1.5152191 4.1740177 7.1527742 2.8992215 4.5322934 6.7180530
    [19] 0.0000000 1.1914052 3.6152333 0.0000000 0.3778717 0.0000000 1.4940469
    

    Benchmark wars!

    @James has found an even faster method and left it in a comment. I upvoted him, if only because I know his victory will be short-lived.

    First, I try compiling, but that doesn’t seem to help anyone:

    p <- rnorm(10000)
    gsk3 <- function(x) { x[x<0] <- 0; x }
    jmsigner <- function(x) ifelse(x<0, 0, x)
    joshua <- function(x) pmin(x,0)
    james <- function(x) (abs(x)+x)/2
    library(compiler)
    gsk3.c <- cmpfun(gsk3)
    jmsigner.c <- cmpfun(jmsigner)
    joshua.c <- cmpfun(joshua)
    james.c <- cmpfun(james)
    
    microbenchmark(joshua(p),joshua.c(p),gsk3(p),gsk3.c(p),jmsigner(p),james(p),jmsigner.c(p),james.c(p))
               expr      min        lq    median        uq      max
    1     gsk3.c(p)  251.782  255.0515  266.8685  269.5205  457.998
    2       gsk3(p)  256.262  261.6105  270.7340  281.3560 2940.486
    3    james.c(p)   38.418   41.3770   43.3020   45.6160  132.342
    4      james(p)   38.934   42.1965   43.5700   47.2085 4524.303
    5 jmsigner.c(p) 2047.739 2145.9915 2198.6170 2291.8475 4879.418
    6   jmsigner(p) 2047.502 2169.9555 2258.6225 2405.0730 5064.334
    7   joshua.c(p)  237.008  244.3570  251.7375  265.2545  376.684
    8     joshua(p)  237.545  244.8635  255.1690  271.9910  430.566
    

    compiled comparison

    But wait! Dirk wrote this Rcpp thing. Can a complete C++ incompetent read his JSS paper, adapt his example, and write the fastest function of them all? Stay tuned, dear listeners.

    library(inline)
    cpp_if_src <- '
      Rcpp::NumericVector xa(a);
      int n_xa = xa.size();
      for(int i=0; i < n_xa; i++) {
        if(xa[i]<0) xa[i] = 0;
      }
      return xa;
    '
    cpp_if <- cxxfunction(signature(a="numeric"), cpp_if_src, plugin="Rcpp")
    microbenchmark(joshua(p),joshua.c(p),gsk3(p),gsk3.c(p),jmsigner(p),james(p),jmsigner.c(p),james.c(p), cpp_if(p))
             expr      min        lq    median        uq       max
    1   cpp_if(p)    8.233   10.4865   11.6000   12.4090    69.512
    2     gsk3(p)  170.572  172.7975  175.0515  182.4035  2515.870
    3    james(p)   37.074   39.6955   40.5720   42.1965  2396.758
    4 jmsigner(p) 1110.313 1118.9445 1133.4725 1164.2305 65942.680
    5   joshua(p)  237.135  240.1655  243.3990  250.3660  2597.429
    

    with rcpp comparison

    That’s affirmative, captain.

    This modifies the input p even if you don’t assign to it. If you want to avoid that behavior, you have to clone:

    cpp_ifclone_src <- '
      Rcpp::NumericVector xa(Rcpp::clone(a));
      int n_xa = xa.size();
      for(int i=0; i < n_xa; i++) {
        if(xa[i]<0) xa[i] = 0;
      }
      return xa;
    '
    cpp_ifclone <- cxxfunction(signature(a="numeric"), cpp_ifclone_src, plugin="Rcpp")
    

    Which unfortunately kills the speed advantage.

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

Sidebar

Related Questions

I want to get all values of a set interface in one go as
I want set Listbox background to transparent but not working Is there any idea?
Sounds simple enough, but its not working. In this example, I want to set
I want to set the Expires header for all image/* and text/css . I'm
I have a base class for all my textboxes and I want to set
I want all my inno set up pages to look like below :
I want to use all the photos in a Flickr set and in order
I want to retrieve a set of results, which consist of all results produced
I have a table groupdentlink where I want to delete all the rows that
I want to store all values of a certain variable in a dataset and

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.