**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

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 ==.

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

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.

**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

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

There is a repeat function, below is an example.

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

**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.

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.

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.

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

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

**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.

**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.

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.

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

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

Former MemberPost authorHi

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

Thanks.