Basics in R
1. Introduction to R
R was developed by statistician and data analyst as an interactive environment for data analysis. It is an integrated suite of software facilities for data manipulation, calculation and graphical display.
2. Why R???
There are several softwares for statistical analysis like SPSS, STATA etc... If so why to use R? Some reasons are listed below:
3. Set up R in your computer:
For Windows:
To Install R:
- Open an internet browser and go to www.r-project.org.
- Click the "download R" link in the middle of the page under "Getting Started."
- Select a CRAN location (a mirror site) and click the corresponding link.
- Click on the "Download R for Windows" link at the top of the page.
- Click on the "install R for the first time" link at the top of the page.
- Click "Download R for Windows" and save the executable file somewhere on your computer. Run the .exe file and follow the installation instructions.
- Now, R is installed, you need to download and install RStudio.
Download Console R
To Install RStudio
- Go to www.rstudio.com and click on the "Download RStudio" button.
- Click on "Download RStudio Desktop".
- Click on the version recommended for your system, or the latest Windows version, and save the executable file. Run the .exe file and follow the installation instructions.
Install both files one after another as you install other apps in your computer
For MAC:
To Install R
- Open an internet browser and go to www.r-project.org.
- Click the "download R" link in the middle of the page under "Getting Started."
- Select a CRAN location (a mirror site) and click the corresponding link.
- Click on the "Download R for (Mac) OS X" link at the top of the page.
- Click on the file containing the latest version of R under "Files."
- Save the .pkg file, double-click it to open, and follow the installation instructions.
- Now that R is installed, you need to download and install RStudio.
To Install RStudio
- Go to www.rstudio.com and click on the "Download RStudio" button.
- Click on "Download RStudio Desktop".
- Click on the version recommended for your system, or the latest Mac version, save the .dmg file on your computer, double-click it to open, and then drag and drop it to your applications folder.
4. Lets get familiarize with R-studio
R Studio is an integrated development environment (IDE) for R.
Drop-down Menus: Drop down menus at the top of the window provide menu options that allow you to maneuver in R Studio a bit without writing out lines of code.
Console: You can type commands in the console as you do in the text editor, as well as see output ( same as R)
R Studio: Top-Right (Environment, History, Tutorials)
- Environment: The Environment tab stores and shows all of the active objects, values, and functions (and anything else you create) during your R session
- R stores both data and output from data analysis (as well as everything else) in objects. Once you create an object, it should appear in the RStudio Environment pane.
- You can also look at your History if you’ve lost a command or variable that you want back. It’s searchable to make finding that missing command super easy!
R Studio: Top-Left (Text editor, R-script, R-markdown, Quarto)
- This is where you write in the script you want to run. You can type multiple lines of code into the text editor without having each line evaluated by R.
- You can save and share the code!
- There are a few other ways to write R scripts, that may be useful in the future. (Such as Rmarkdown, R notebooks, or making slides.)
R Studio: Bottom left (R Console)
- The R console is the pane where R is waiting for us to tell it what to do, and where it will show the results of the commands.
- We can type directly into console, but they will be forgotten once we close the session.
R Studio: Bottom Right
- Plots: Any plots that you produce be in the “Plots” tab. It will keep a history so you can see changes as you make them.
- Packages: You can download new packages by clicking Packages -> Install. You can load packages by checking them off, but this is best done in your scripts!
- Help: Have a question on syntax of a command? You can look at it’s help page by searching for it. You can also poke around tutorials and FAQ.
- Working Directory: Files in the working directory, you can change your working directory to anything else in the code or by going to that location and clicking More -> Set as Directory.
5. Before Getting into R, Lets learn basics
What is Function?
A function is a sequence of program instructions that perform a specific task.
Function Components
The different parts of a function are −
Function Name − This is the actual name of the function. It is stored in R environment as an object with this name.
Arguments − An argument is a placeholder. When a function is invoked, you pass a value to the argument. Arguments are optional; that is, a function may contain no arguments. Also arguments can have default values.
Function Body − The function body contains a collection of statements that defines what the function does.
Return Value − The return value of a function is the last expression in the function body to be evaluated.
6. Lets dive into R
First of all open R-studio. Thereafter from top-left section, open R-script.
Set and Get Working Directory
In order to set working directory
Code: setwd(“path to directory”)
Example:setwd(“D:\Folder1\DH\Research\SEAP\analysis”)
In order to get working directory
Code: getwd()
Example:getwd()
7. Basic Arithmetic and Coding in R( R as a calculator)
- Math Operation
- 2 + 2 #addition
- 5 %% 2 #remainder
- 3.452 * 6 #multiplication
- 2^4 #power
- (3 + 4)*6 #Simplification
- Functions
- log(10) #natural log of 10
- log(10, base = 10) #log with base of 10
- sqrt(16) #square root
- exp(10) #exponential
- seq(1, 10, 2) #generate sequence
- Nesting of functions is possible
- log(exp(3)) #natural log of exponential of x
- sqrt(abs(-25)) #square root of absolute value of x
- When to use # , <- , &, | , ==, <, >, <=, >= , T, TRUE, F, FALSE?
- # is used to add comment and text after it is considered as comment
- <- is assignment operator. It is similar to “=”
- & is AND operator
- | is OR operator
- == is to compare if two items are equal
- > is "Greater than"
- < is "Less than"
- <= is to compare if one item is less than or equal to other
- >= is to compare one item is more than or equal to other
- T or TRUE: Logical operator
- F or FALSE: Logical operator
- Details of Assignment operator (<- or -> )
- x<-4
- x+10
- y<-"Hello world"
- print(y)
- "Nepal"->country
Question: What is the difference between <- and -> ???
7. Install packages in R
You can install packages through directly through cran or manually.
- Visual Method: In order install packages: Tools>Install Packages>Install
- Code:
- install.packages(“package_name”)
- Example
- install.packages("tidyverse")
#NB Note use of "____" in installation of package
List of some important packages
8. How to use installed packages in R after installation?
- Code:
- library(package_name)
- require(package_name)
- attach(package_name)
- Example:
- library(tidyverse)
- attach(tidyverse)
- require(tidyverse)
10. How to seek help in R?
- Way-1: In order to get help in R: throw single or double ? mark in front of function.
- Way-2: Use Books
- Way-3: Use cheat sheets of different packages
- Way-4: Google Search
- Way-5: Ask questions in Stackoverflow
- Way-6: Ask those who knows