Skip to Content
Author's profile photo Louis de Gouveia

From R to Custom PA Component Part 2

Intro

This is part 2 of a series of blogs.

Please refer to part 1 that can be found here From R to Custom PA Component Part 1

In this blog I will be focusing on beginner syntax that is required to create a Predictive Analytics component, will cover the following

  • Basic syntax
  • Functions
  • Variables
  • Help

We will then have a look at RStudio again to understand the difference between RStudio and RGui.

Basic Syntax

So open the RGui as shown in part 1 of my this blog series. You can immediately use it as a calculator by typing in 4+4 and then pressing enter. Then 8 will then be shown as the answer. As seen below I have done +, / and *. Give it a try and see the same results as shown below

RGui1.jpg

You can then also do Boolean expressions by typing 5<7, then pressing enter. Will respond with TRUE or FALSE. In this case TRUE. Note to test if 8=7+1, in R = is shown as ==.

RGui2.png

In R, the value T is short syntax for TRUE. Same for the value F being short for FALSE.

RGui3.png

When working with text you should enter the text in double quotes. As shown below I just typed the text and got an error back. The second time I enterd the text correctly. With text it just responds with what you have entered.

RGui4.jpg

Functions

R comes with built in functions. So lets go through some of the functions to see how they work.

To use the function sum, type sum(value1,value2,value3,etc.). Below is some examples

RGui5.jpg

To use the square root function, type sqrt(value1)

RGui6.jpg

There is a repeat function, below is an example.

RGUI7.jpg

More examples of functions. Give them a try. Feel free to google some more functions.

RGui8.jpg

Variables

When working with variables, most programming languages you need to declare the variable, say whether it is string or integer. In R you don’t need to do that. You can just type x<-10. This syntax is saying the variable is X, we assigning the value 10 to X.

So below you can se I assigned X to have the value 10, after pressing enter the console is waiting for another command. If you just type X again it will tell you the value in X.

RGui9.jpg

You can then start working with x. So if x has a value of 10, then if I want to divide by 2 you just simply type X/2, then press enter. Below you can see I have divided by 2, later on I add 10. Bear in mind I’m not changing X value, I’m just calculating what X would be should I divide by 2 or add 10 to it. X still has the value 10.

RGui10.jpg

To change the value you must use <- to assign the value. So below I’m saying X has a new value that is X/2 which is 5.

RGui11.jpg

We can do the same with text. Here I have a variable called Y, I then assign the value Hello World to it.

RGui12.jpg

You can check the data types R has assigned to x and y by using str function. Will show the type and the value.

RGui13.jpg

Help

You can also use the help function to get more info. So to get more help just type help(function), so below I have typed help(sum). This will then launch a browser that shows help for this function.

Rgui14.jpg

RStudio Differences

So as mentioned in part 1 of my blog series we can code in the RGui or RStudio. So lets see what RStudio offers.

So in the R Console we can type the commands the same as we did in the RGui console. By looking at the top right window we can then see that it shows the X and Y variables and values. So in the RGui we have to call x and y to see the value where in the RStudio we can see it immediately in the top right hand window. The bottom right window you can access the help.

RConsole1.jpg

When working in the console you can press ctrl+space bar and then RStudio will assist with available functions or syntax with regard to the text you typed. Will also provide info on how to use the function. So for example below we can see for substr it requires x which is the string, then a start value, and a stop value.

RStudio2.jpg

Hope this helps, part 3 can be found here From R to Custom PA Component Part 3.

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Mikel Makmerphy
      Mikel Makmerphy

      Nice try, My first steps in R was swirl: Learn R, in R.

      Author's profile photo Louis de Gouveia
      Louis de Gouveia
      Blog Post Author

      Hi

      Thanks for the feedback. I haven't used swirl myself, will have a look. Maybe we can add it to the list.

      Thanks.