08 September 2014

Part 2 StartingR (R for beginners)

Terminology:

The following is a list of terms used in this book:
  • R Base | R Base
  • R Studio | R Studio
  • Syntax R | Syntax
  • Interface | antarmuka
  • Icon | ikon
  • Window | jendela
  • R-prompt | R-prompt
  • R-script | R-script
  • R-console | R-konsol
  • R-code | R-kode
  • Arguments | perintah tambahan

Part 2: Starting-R

Getting excited yet? Now let’s try to run R. Open your applications list or window. The following figure shows a list of my apps on Mac OS. Look for the two R icons on the left. The left icon would be for R Studio, while the right one would run R Base.
Figure 2.1 Icons of R Studio and R base

2.1 Interface of R Base

Try to click the right R icon. Then this window will pop up.
Figure 2.2 Window of R Base
R Base is the engine of R. It has a very simple interface in form of console window. In the window, you would see the following symbol, which is called R-prompt.
>
You would type all of your R commands behind that prompt. Now try to type some codes below. Ends with enter for each command.
> 1+1
There you go. It is your first R code ever. You should remember this date. Mine was in April 2011.
  • Then type this code. See what happens.
> mtcars
  • Then this one. See what happens. The mtcars is a data frame that comes in the R installation by default as an example. There are several other installed data frames for exercise purposes.
> plot(mtcars) 
Again, you must remember this date. It is your first R plot ever.
After entering the plot() command you would see another window pops up containing your plot.

2.2 Interface of R Studio

Now go back to your app window. It’s time to click the R Studio icon. You would see this window opens up.
Figure 2.3 Window R Studio
As I mentioned in Part 1, R Studio is a integrated development environment (IDE) for R. There are four windows in the interface. The position is changeable according to your taste. On my system, the windows in clock-wise order are:
  • R-script Window: you type your code here.
  • R-console Window: here, you can also type you code and see the result immediately.
  • R-environment Window : you can see your data frames, variables, objects, and other components you are using in your work. All of these components are loaded on your memory (RAM).
  • R-files, plots, packages, help, viewer:
  • tab “files”: you can see list of your files in the working directory.
  • tab “plots”: it contains a history of your plots.
  • tab “packages”: you can see the installed packages in your system.
  • tab “help”: you can see the help file you need inhere.
  • tab “viewer”: this tab is used to open a website. But you will rarely (or never) use this tab.
Writing in the script and console window: what’s the difference?
Let’s try:
  • Copy the above commands that you just typed in to the console window. Don’t forget to hit enter for each command. What happens? You would see the result immediately after you hit enter.
  • Now paste the same commands in to the script window. What happens? You would see that you can copy-paste the commands back and forth without having to run them.
So how to run the list of commands?
  • You can click the Run button on the top toolbar.
  • The other way is by pressing the following combination keys together:
  • in MacOS: hit command(cmd) + enter.
  • in Linux dan Windows: hit control(ctrl) + enter.
  • Try it. Then see the results in the console window.

2.2 R syntax

Like other programming language, R also has a syntax or how to write the code. As you type the above codes, you have learned the syntax indirectly. So here are some notes:
  • Type the commands after the prompt symbol >
  • What do you type? You have typed a simple calculation and asked R to calculate the result. Or, you can type commands or functions. So what are they?
Let’s think of a simple story. Say you want to ask somebody to buy a sachet of coffee. You would say something like,
"Could you buy coffee for me".
Is that statement enough?
It’s enough, if you drink any kind of coffee. There would be many variations of result that you are going to get. You can get a cup of coffee, or you can get a sachet of coffee.
So what is the solution?
You have to give specific supplementary instructions. In R this is called arguments. So your command could be something like this:
Could you buy me a cup of black coffee at the near by coffee stall, medium size, with there sachets of sugar.
Your command is specific this time, that can be listed as follow:
  • Your command = buy
  • Your arguments :
  • object = beverage + coffee
  • type:
    • black
    • medium
  • location = near by stall
  • quantity = a cup
  • sugar = yes, 3 sachets
In R you can write the imaginary command like this:
> buy(beverage, type = c(coffee, black, medium), loc = "stall", sugar = "TRUE")
If you don’t know how to write a command, just type > ?. In this case, you would type.
> ?buy
Then you would see the help window shows the following documentation of the (imaginary) command:
buy

Description

A generic function to buy coffee. For more details about the graphical parameter arguments, see the following explanation. 

Usage

buy(class, type=c(..., ..., ...), loc="...", sugar="...")

Arguments

class: classification of your purchase.

  beverage
  cleaning
  
type: the specification of your product

  coffee for coffee products 
  cleaning for cleaning products
  black for black coffee
  flatwhite for flatwhite coffee
  tall  for tall size
  medium for medium size
  small for small size
  fluid for fluid cleaning products
  bar for bar cleaning products

loc: the location/lokasi barang yang anda perlukan

  stall 
  mall
  
sugar:

  TRUE with sugar
  FALSE without sugar
By looking at the documentation, you can learn how to write the code and ask R to do your analysis. In the next part we will learn more about basic R commands. This time we will play around with the real R commands.
<!-- dynamically load mathjax for compatibility with self-contained
-->
Post a Comment