Using ‘for’ Loops in R

In our last post on R, we looked at using the ‘switch’ function. The problem with the ‘switch’ function is that you have to input every data point manually. For example, if you have five different values you want to calculate in a function you need to input each value manually until you have calculated all five.

There is a way around this and it involves the use of a ‘for’ loop. A ‘for’ loop is used to repeat an action in R in a vector. This allows you to do a repeated action with functions that cannot use vectors.

We will return to are ‘CashDonate’  that has been used during this discussion of programming. Below is the code from R as it was last modified for the previous post.

CashDonate <- function(points, GameType){
 JamesDonate <- points * ifelse(points > 100, 30, 40)
 OwnerRate<- switch(GameType, Home = 1.5, Away = 1.3, International = 2)
 totalDonation<-JamesDonate * OwnerRate
 round(totalDonation)
}

The code above allows you to calculate how much money James and the owner will give based on points scored and whether it is a home, away, or international game. Remember, the problem with this code is that each value must be entered manually in the “CashDonate” function. To fix this we will used a ‘for’ loop as seen in the example below.

CashDonate <- function(points, GameType){
 JamesDonate <- points * ifelse(points > 100, 30, 40)
  OwnerRate <- numeric(0)
 for(i in GameType){
 OwnerRate<- c(OwnerRate, switch(i, GameType, Home = 1.5, Away = 1.3, International = 2))}
 totalDonation<-JamesDonate * OwnerRate
 round(totalDonation)
}

There are some slight yet significant changes to the function as explained below

  1. The variable ‘OwnerRate’ is initially set to be a numeric variable with nothing inside it (0). This because different values will be put into this variable based on the ‘for’ loop.
  2. Next, we have the ‘for’ loop. “i” represent whatever value is found in the argument ‘GameType’ the choices are home, away, and international. For every value in the dataframe the ‘CashDonate’ function will check the’ GameType’ to decide what to put into the ‘OwnerRate’ variable.
  3. Next, we have the ‘OwnerRate’ variable again. We use the ‘c’ function to combine OwnerRate (which currently has nothing in it), with the “switch” function. The switch function looks at ‘i’ (from the ‘for’ loop) and calculates the same information as before.
  4. The rest of the code is the same.

We will now try to use this modified ‘CashDonate’ function. But first we need a dataframe with several data points to run it. Input the follow dataframe into R.

GameInfo<- data.frame( points = c(101, 115, 98, 89, 104), GameType = c("Home", "Away", "International", "Away", "Home"))

In order to use the ‘CashDonate’ function with the ‘for’ loop you have to extract the values from the ‘GameInfo’ dataframe use $ sign. Below is the code with the answer from the ‘CashDonate’ function

CashDonate(GameInfo$points, GameInfo$GameType)
[1] 4545 4485 7840 4628 4680

You can check the values for yourself manually. The main advantage of the for loop is that we were able to calculate all five values at the same time rather than one at a time.

Advertisements

2 thoughts on “Using ‘for’ Loops in R

  1. Pingback: Using 'for' Loops in R | Education and Research...

  2. Pingback: Apply Functions in R | educationalresearchtechniques

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s