Monday, March 8, 2021
  • Setup menu at Appearance » Menus and assign menu to Top Bar Navigation
Advertisement
  • AI Development
    • Artificial Intelligence
    • Machine Learning
    • Neural Networks
    • Learn to Code
  • Data
    • Blockchain
    • Big Data
    • Data Science
  • IT Security
    • Internet Privacy
    • Internet Security
  • Marketing
    • Digital Marketing
    • Marketing Technology
  • Technology Companies
  • Crypto News
No Result
View All Result
NikolaNews
  • AI Development
    • Artificial Intelligence
    • Machine Learning
    • Neural Networks
    • Learn to Code
  • Data
    • Blockchain
    • Big Data
    • Data Science
  • IT Security
    • Internet Privacy
    • Internet Security
  • Marketing
    • Digital Marketing
    • Marketing Technology
  • Technology Companies
  • Crypto News
No Result
View All Result
NikolaNews
No Result
View All Result
Home Data Science

Different kinds of loops in R.

August 20, 2019
in Data Science
Discover how machine learning can solve finance industry challenges by Jannes Klaas
585
SHARES
3.3k
VIEWS
Share on FacebookShare on Twitter

Normally, it is better to avoid loops in R. But for highly individual tasks a vectorization is not always possible. Hence, a loop is needed – if the problem is decomposable.

Which different kinds of loops exist in R and which one to use in which situation?

You might also like

How Spotify Uses Artificial Intelligence, Big Data, and Machine Learning

Top 6 Regression Techniques a Data Science Specialist Needs to Know

An Easy Way to Solve Complex Optimization Problems in Machine Learning

In each programming language, for- and while-loops (sometimes until-loops) exist. These loops are sequential and not that fast – in R.

for(i in x)

{task}

 

i=y

while(i<=x)

{task

i=i+1}

 

Even for prototyping sometimes too slow.

But how to improve speed?

There are three options in R:

  1. apply loops
  2. parallelization
  3. RCPP

apply loops:

Normally, you can use apply for calculating some standard statistics of the columns, the rows, or both. But you can use a trick to adjust the apply order for a loop. The syntax is:

F <- function(i, x, y, z,…)

{task}

apply(as.data.frame(1:length(vector)), margin = 1, FUN = F)

In this case you use the vector not for direct calculation but as an index “i” instead.

The sapply order is even faster.

F <- function(i, x, y, z,…)

{task}

sapply(1:length(vector), FUN = F)

Parallelization:

You can use loops and apply orders also in parallel. You need:

library(“doParallel”)

library(“parallel”)

library(“foreach”)

 

Firstly defining the number of cores. Leave out at least one:

 

NumOfCores <- detectCores() – 1

registerDoParallel(NumOfCores)

 

Either using a loop:

 

foreach::foreach(x = 1:length(vector), .combine = rbind, .inorder = T, .multicombine = F) %dopar%

{task}

 

This loop creates a vector of results.

If the order is not important you can increase performance by .inorder = F. This means that a free processor takes the next iteration independent from the sequence of the iterations.

 

Or using a parSapply order:

 

clusters <- makeCluster(NumOfCores)

parSapply(cl = clusters, X = 1:length(vector), FUN = F, x = x, y = y, z = z,… )

 

In this case it is important to integrate the data in reference within the parentheses – you cannot directly connect to the workspace like in the ordinary sapply order.

 

RCPP:

 

Firstly you need to install RTools.

 

library(“Rcpp”)

 

define a function in C++, create a shared library and compile the code.

 

#include <Rcpp.h>

using namespace Rcpp;

                                  

// [[Rcpp::export]]

double NameOfFunction (NumericVector Vector)

{task}

 

Then you can call it in R:

 

sapply(X = 1:length(testVec), FUN = NameOfFunction, y = Vector)

 

But when to use which kind of loop?

 

Judging from the experience, I recommend to make the decision dependent from the number of iterations and the costs of each iteration.

 

 

Not costly

costly

Low number of iterations

for-loop, while-loop

RCPP, foreach

Large number of iterations

RCPP, sapply, apply, lapply

parSapply, RCPP

 

 


Credit: Data Science Central By: Frank Raulf

Previous Post

Beefy Leaked Specs Hint at PlayStation 5’s Price Tag

Next Post

How Activity Logs Help WordPress Admins Better Manage Website Security

Related Posts

How Spotify Uses Artificial Intelligence, Big Data, and Machine Learning
Data Science

How Spotify Uses Artificial Intelligence, Big Data, and Machine Learning

March 8, 2021
Top 6 Regression Techniques a Data Science Specialist Needs to Know
Data Science

Top 6 Regression Techniques a Data Science Specialist Needs to Know

March 8, 2021
An Easy Way to Solve Complex Optimization Problems in Machine Learning
Data Science

An Easy Way to Solve Complex Optimization Problems in Machine Learning

March 8, 2021
A Plethora of Machine Learning Articles: Part 2
Data Science

A Plethora of Machine Learning Articles: Part 2

March 4, 2021
The Effect IoT Has Had on Software Testing
Data Science

The Effect IoT Has Had on Software Testing

March 3, 2021
Next Post
How Activity Logs Help WordPress Admins Better Manage Website Security

How Activity Logs Help WordPress Admins Better Manage Website Security

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Plasticity in Deep Learning: Dynamic Adaptations for AI Self-Driving Cars

Plasticity in Deep Learning: Dynamic Adaptations for AI Self-Driving Cars

January 6, 2019
Microsoft, Google Use Artificial Intelligence to Fight Hackers

Microsoft, Google Use Artificial Intelligence to Fight Hackers

January 6, 2019

Categories

  • Artificial Intelligence
  • Big Data
  • Blockchain
  • Crypto News
  • Data Science
  • Digital Marketing
  • Internet Privacy
  • Internet Security
  • Learn to Code
  • Machine Learning
  • Marketing Technology
  • Neural Networks
  • Technology Companies

Don't miss it

Everything you need to know about Microsoft Exchange Server hack
Internet Security

Everything you need to know about Microsoft Exchange Server hack

March 8, 2021
Iranian Hackers Using Remote Utilities Software to Spy On Its Targets
Internet Privacy

Iranian Hackers Using Remote Utilities Software to Spy On Its Targets

March 8, 2021
How Spotify Uses Artificial Intelligence, Big Data, and Machine Learning
Data Science

How Spotify Uses Artificial Intelligence, Big Data, and Machine Learning

March 8, 2021
Raspberry Pi to get machine learning boost
Machine Learning

Raspberry Pi to get machine learning boost

March 8, 2021
Top 6 Regression Techniques a Data Science Specialist Needs to Know
Data Science

Top 6 Regression Techniques a Data Science Specialist Needs to Know

March 8, 2021
Dataiku named as Gartner Leader for Data Science and Machine Learning
Machine Learning

Dataiku named as Gartner Leader for Data Science and Machine Learning

March 8, 2021
NikolaNews

NikolaNews.com is an online News Portal which aims to share news about blockchain, AI, Big Data, and Data Privacy and more!

What’s New Here?

  • Everything you need to know about Microsoft Exchange Server hack March 8, 2021
  • Iranian Hackers Using Remote Utilities Software to Spy On Its Targets March 8, 2021
  • How Spotify Uses Artificial Intelligence, Big Data, and Machine Learning March 8, 2021
  • Raspberry Pi to get machine learning boost March 8, 2021

Subscribe to get more!

© 2019 NikolaNews.com - Global Tech Updates

No Result
View All Result
  • AI Development
    • Artificial Intelligence
    • Machine Learning
    • Neural Networks
    • Learn to Code
  • Data
    • Blockchain
    • Big Data
    • Data Science
  • IT Security
    • Internet Privacy
    • Internet Security
  • Marketing
    • Digital Marketing
    • Marketing Technology
  • Technology Companies
  • Crypto News

© 2019 NikolaNews.com - Global Tech Updates