**ECONinfo » Rblogger**, and kindly contributed to R-bloggers)

Okay, as I wrote yesterday, ifelse is rather slow, at least compared to working in C++. As my current project is using ifelse rather a lot, i decided to write a small utility function. In the expectation that I will collect a number of similar functions, I made a package out of it and posted it on github: https://github.com/ojessen/ojUtils

I get a speedup of about 30 times, independent of the target type.

Feedback and corrections greatly appreciated.

Thanks to the people at Travis for providing a free CI server which works directly with github. This of course is a tiny example, but it is good to know that the workflow to set this up can be done in 5 minutes.

And thanks to Romain Fraoncois for showing some Rcpp sugar:

class=”twitter-tweet” lang=”de”>

.@ojessen I sent you a pull request leveraging Rcpp’s ifelse. Although maybe wait: Rcpp’s ifelse is broken … https://t.co/38P0mQc3aZ

— Romain Francois (@romain_francois) 20. Juni 2014

Some data:

# ifelseR.R

*post_000*

*Fri Jun 20 17:12:25 2014*

`require(ojUtils)`

`## Loading required package: ojUtils`

`require(microbenchmark)`

`## Loading required package: microbenchmark`

```
test = sample(c(T,F), size = 1e5, T)
yes = runif(1e5)
no = runif(1e5)
microbenchmark(ifelse(test, yes, no), ifelseC(test, yes, no))
```

`## Loading required package: Rcpp`

```
## Unit: microseconds
## expr min lq median uq max neval
## ifelse(test, yes, no) 31925 33404.8 34065.1 58083.5 71891 100
## ifelseC(test, yes, no) 620 647.5 721.8 817.7 209254 100
```

```
test = sample(c(T,F), size = 1e5, T)
yes = rep("a", 1e5)
no = rep("b", 1e5)
microbenchmark(ifelse(test, yes, no), ifelseC(test, yes, no))
```

```
## Unit: milliseconds
## expr min lq median uq max neval
## ifelse(test, yes, no) 57.313 58.763 59.626 72.435 87.92 100
## ifelseC(test, yes, no) 1.747 1.837 1.926 2.749 29.56 100
```

```
test = sample(c(T,F), size = 1e5, T)
yes = rep(1L, 1e5)
no = rep(2L, 1e5)
microbenchmark(ifelse(test, yes, no), ifelseC(test, yes, no))
```

```
## Unit: microseconds
## expr min lq median uq max neval
## ifelse(test, yes, no) 30747.6 31868.5 32274.8 32829.0 59412 100
## ifelseC(test, yes, no) 453.7 548.9 581.5 646.2 27575 100
```

```
test = sample(c(T,F), size = 1e5, T)
yes = rep(T, 1e5)
no = rep(F, 1e5)
microbenchmark(ifelse(test, yes, no), ifelseC(test, yes, no))
```

```
## Unit: microseconds
## expr min lq median uq max neval
## ifelse(test, yes, no) 29331.2 31167.3 31719.7 32455.3 60589 100
## ifelseC(test, yes, no) 460.1 537.1 566.8 640.7 27118 100
```

**leave a comment**for the author, please follow the link and comment on their blog:

**ECONinfo » Rblogger**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...