LAP 1
Introduction to R Programming
1. Definition
R is a free software environment for statistical computing and graphics.
2. How to install R / R Studio
RGUI: It is a bad and old version editor, but it is essential for working on the advanced editor (RStudio).
[Link]
RStudio: it is an advanced editor. In its platform you can edit and visualize the results of coding lines.
[Link]
R Console: This area shows the output of code you run. Also, you can directly write codes in console.
Code entered directly in R console cannot be traced later. This is where R script comes to use.
R Script: As the name suggest, here you get space to write codes. To run those codes, simply select the
line(s) of code and press Ctrl + Enter. Alternatively, you can click on little ‘Run’ button location at top
right corner of R Script.
R environment: This space displays the set of external elements added. This includes data set, variables,
vectors, functions etc. To check if data has been loaded properly in R, always look at this area.
Graphical Output: This space displays the graphs created during exploratory data analysis. Not just
graphs, you could select packages, seek help with embedded R’s official documentation.
3. Essentials of R Programming
3.1 Basic Computations
To get familiar with R coding environment, start with some basic calculations.
3.2 obkects in R
Everything you see or create in R is an object. It can be classified as follows;
3.2.1 classes of objects
There are 5 basic classes of objects. This includes:
- Character
- Numeric (Real Numbers)
- Integer (Whole Numbers)
- Complex
- Logical (True / False)
3.2.2 Data Types in R
R has various type of ‘data types’ which includes vector (numeric, integer etc), matrices, data frames and list. Let’s
understand them one by one.
- One-dim variable: > x=5
- Vector: a vector contains object of same class
Note you can use [] to get a specific element in the vector
- List: A list is a special type of vector which contain elements of different
data types. For example:
Note you can use [] to get a specific element in the list
- Matrices: When a vector is introduced with row and column i.e. a dimension attribute, it becomes a matrix. A
matrix is represented by set of rows and columns. It is a 2 dimensional data structure. It consist of elements of
same class. Let’s create a matrix of 3 rows and 2 columns:
Note you can use [#row,#coloumn] to get a specific element in the matrix
- Data Frame: This is the most commonly used member of data types of family. It is used to store tabular
data. It is different from matrix. In a matrix, every element must have same class. But, in a data frame,
you can put list of vectors containing different classes. This means, every column of a data frame acts
like a list. Every time you will read data in R, it will be stored in the form of a data frame. Hence, it is
important to understand the majorly used commands on data frame:
Note you can use [#row,#coloumn] to get a specific element
in the data frame or use $ to get the whole coloumn
dim() returns the dimension of data frame as 4 rows and 2 columns. str() returns the structure of a data frame i.e.
the list of variables stored in the data frame
in data science, a variable can be categorized into two types: Continuous and Categorical.
Continuous variables are those which can take any form such as 1, 2, 3.5, 4.66 etc. Categorical variables.
However, the categorical variables can be ordinal and nominal (the ordinal variables has a natural order) such as
(high,medium,low) however the nominal variable does not involve any order (elephant,griraff,donkey,horse)