Getting Started With R – First Hands On
Share
Working with R:
R is a case-sensitive language. It distinguished between X and x . As X is capital letter while x is small letter.
We are writing program in Console window. Here , you are doing simple calculation as :
There is a disadvantage of R , we cannot save program codes in file . So , we can use more user friendly tool Rstudio for coding and storing programs.
Working with Rstudio :
When we open Rstudio , it looks like –
We have three windows :
First window shows Console . Here , we can write and execute programs simultaneously.
Second window is upper right. It shows Environment and History window.
Third window is lower right. It shows Files , Plots , Packages, Help and Viewer windows.
We open a new window , called Script window.
For opening a Script window, click on File tab on upper left corner of Rstudio . Then , click on New File and click on R Script. It will look like –
I am so excited to start my first program in Rstudio. First type help() and pres ctrl +R(control + R) to run program. You can also use ctrl+Enter to run the program.
help()
When we execute the code , it shows in console window to execute.
It will open Help window in bottom right window.
It helps us to find help in functions for programming.
Another code :-
help.start()
By running this code, it opens Help window .It shows introduction of R.
You can do simple calculation . Write this code in Script window –
1+1
Output will be shown in Console window:-
[1] 2
Similarly , you can do other calculations as-
2*5
Output –
[1] 10
Assignment Operator:
We have use assignment operator as “<-” & “=” .
Objects :
It is used to store the values . We can create object by assigning values to object.
a<-1
In this program , we create an object named “a” , which store value 1.We are using assignment operator(<-) to assign value to object. We can see “a” object in Environment window. The object a stores 1.
Comments:
It is added to make program easier to understand . We can add comments by adding “#”. It is non-executable code ( it is not execute by R).
You can check out –
Here , # object a creation is a comment . It gives explanation of our code.
Programming basic constraints :
We have two phases while running the program. It is compilation and execution.
Computer only reads binary form . When we run a program, first the program converts into machine understandable language in form of binary(0 or 1) source file. The process is called compilation. After compilation , the binary file is loaded into computer memory for execution .
Every code written in script window is compile and execute . But comments are ignored in compilation and execution.
We can store an object like this also –
b<-1 + 1
It will store 2.
To check “b” object value , we run this code-
b
It displays this output in console window –
[1] 2
We can remove objects from Environment window. We can do it by using rm() :-
rm(a)
You can checkout Environment window. There is no object “a” stored in it.
c()
We can combine multiple values in an object by using c() .
x<-c(1,2)
It creates x object , which stores 1 and 2.