473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,326 developers and data experts.

Lesson 5 (part1) – r programming structures

nbiswas
149 100+
The programming structures that we will examine include: control flow statements and user defined functions.
In previous lessons we were either performing summary or descriptive statistics across single variables, but in this lesson we will examine the interrelationships between variables. Interrelationship analysis comes in different forms and in this lesson we will examine covariance, correlation, and linear regression techniques.
You have probably discovered that as we write more complex R scripts there are scenarios that require decision making and R provides many flexible options to control the flow of execution using conditional and iteration expressions.
We also mentioned that R is a functional and an object-oriented programming language. In this lesson we will examine how we define and use functions during our analysis.
We will learn how to create different types of R functions.
We have learned how to examine data trends over a single variable using a histogram or over 2 variables using scatterplots. In the final part of this lesson we will learn how to analyze the relationship between 2 variables through learning about covariance, correlation, and linear regression.
Let's examine decision making in R.
In this example we generate 1 random number between 1 and 100 using the runif() or random uniform distribution function. This function returns a floating point value and in this scenario we would like to simply deal with integer values so we use as.integer() function to discard the decimal portion of our number.
To enable reproducible results we have decided in line 2 to set a static seed value to R's pseudo-random number generator prior to the request for a number.
If you would like randomness when this script is executed you would obviously use a different seed value each time.
The purpose of the script is to print a statement that the random number is either odd or even.
We state the conditional expression inside parenthesis following the if statement. Here we are checking if the result of applying the modulus 2 operator to the number is zero (0) or not. If there is no remainder then we know that the number is indeed even and R will execute the code defined within the curly {} brackets.
If the first conditional expression is evaluated FALSE then the next conditional expression will be tested or in this case the final else clause will be executed.
Note the location of the brackets and indentation shown in this example. This example is consistent with various R coding style guidelines.
Throughout this lesson we will be using simulated data.
R has many methods of creating data that can be used for analysis. Two of the most common types of distributions are uniform distributions and normalized distributions.
The functions runif and rnorm can be used to generate a vector of values with the corresponding distribution properties.
With normal distributions you provide a mean and a standard deviation. For example, if you wanted to create a large data set of 100 possible test scores with a normal distribution around a mean mark of 75 and a standard deviation or measure of distribution of 2 you could use the example as shown. To visualize and verify your data distribution you could use the plot or hist functions.
As with most functional programming languages R provides program structures to control iteration or looping behaviour. We will examine each of these iteration operations in this lesson.
The repeat statement can be used to define a block of statements that will continue to iterate indefinitely.
The break statement is used to exit the loop. In this example we simply check that the value stored in the variable x is greater than 9 we exit the loop otherwise we will print out the current value of x, increment the value by one and the loop repeats. When the value stored in the variable x is greater than 9 we use the break statement to leave the loop and continue in the script.
A conditional looping structure defines the initial or precondition within parenthesis.
In this scenario our entry condition to the looping code involves checking if the value stored in the variable curr is less than or equal to the number of marks in our integer vector called marks.
Recall that in R code blocks are usually defined using the curly or brace brackets {}. It is possible to avoid using brace brackets if the expression is a single line and another option is to combine multiple expressions on a single line using the semi-colon (:). In this example we are actually doing both of these techniques. This may seem confusing so style guidelines should be considered if you have multiple developers working on a single project.
On line 3, we initialized the value of the variables p and curr on a single line.
On line 6, we use an if statement to check if a value in the marks vector is greater than or equal to 50, if it is then we increment the value in the p variable by one.
On line 7 the if statement above is considered a complete expression and therefore the code on line 7 will execute for each iteration.
Once the looping condition is no longer true the msg will be generated using the sprintf() function and then sent to the standard output display using the cat() function.
When the iteration scenario has a well-defined number of iterations, a for statement can be used in R.
The first example uses a sequence function to create a temporary vector of values. The value of i will begin with a value of 5 and then the value will be incremented by 5 until it reaches 15 for the final iteration.
The second example iterates over an existing vector of integers and since the index value of the marks vector is not required we can use a simplified version of the for statement. As we iterate through the values, the counter p will be incremented when the value in 50 or more. This style of for loop can be used to iterate over any vector data structure and it will always examine each element of the vector from the first position to the final position.
Sep 4 '14 #1
1 5022
zmbd
5,501 Expert Mod 4TB
nbiswas:
Please provide proper citations for these articles.
-z
Sep 29 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: christopher diggins | last post by:
I have written an article on how to do Aspect Oriented Programming in vanilla C++ (i.e. without language extensions or other tools such as AspectC++). The article is available at...
65
by: Roger Smythe | last post by:
A means for the progressive decomposition a problem space into increasingly simpler component parts such that these component parts represent higher levels of conceptual abstraction, and are...
25
by: Justin Robbs | last post by:
I am trying to write the communcations part of a Point of Sale program for the Convenience Store industry. The setup in each store will have varying numbers of registers. There could be as few...
15
by: UrsusMaximus | last post by:
While preparing a Python411 podcast about classes and OOP, my mind wondered far afield. I found myself constructing an extended metaphor or analogy between the way programs are organized and...
3
by: Juan R. | last post by:
In http://canonicalscience.blogspot.com/2006/04/scientific-language-canonml-is.html] I presented some generic requirements for a markup language for science and mathematics. Basic features of...
1
by: Tina888 | last post by:
I am a very new programmer. Trying to solve this problem. Create a static function called Part1 with the following signature: public static void Part1() Call this...
151
by: istillshine | last post by:
There are many languages around: C++, JAVA, PASCAL, and so on. I tried to learn C++ and JAVA, but ended up criticizing them. Is it because C was my first programming language? I like C...
1
nbiswas
by: nbiswas | last post by:
In this lesson we will initially learn about the features and uses of R. R is a software environment that is excellent for data analysis and graphics. It was initially created in 1993 by Ross...
1
nbiswas
by: nbiswas | last post by:
Welcome to the lesson on R data structures. To perform any meaningful data analysis we need to collect our data into R data structures. In this lesson we will explore the most frequently used...
1
nbiswas
by: nbiswas | last post by:
In this lesson we will learn how to import external data from files into R. We will also learn how to export, or write, the data to files if needed. Once we have our data loaded into memory we can...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.