0% found this document useful (0 votes)
15 views20 pages

R Programming

Uploaded by

Ganesh Shejule
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
15 views20 pages

R Programming

Uploaded by

Ganesh Shejule
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 20

What is R- Studio?

 Before discussing what RStudio is and why to use it, let's


first give a definition of R.
 R is a popular programming language and free and open-
source software used in data analysis and data science.
 It's especially powerful in performing advanced statistical
computing and creating compelling plots.
 R provides more than 18,000 dedicated data science
packages (as of September 2022), both multipurpose and
narrowly-specialized ones.
 It’s a technology that’s well-supported by an active and
helpful online community and is compatible with various
operating systems.
 RStudio offers numerous helpful
features:
 A user-friendly interface
 The ability to write and save reusable scripts
 Easy access to all the imported data and created
objects (like variables, functions, etc.)
 Exhaustive help on any object
 Code autocompletion
 The ability to create projects to organize and share
your work with your collaborators more efficiently
 Plot previewing
 Easy switching between terminal and console
 Operational history tracking
 Plenty of articles from RStudio Support on how to
use the IDE
How to Install RStudio
To install and start working in RStudio, we need first to
download and install the R programming
language itself. To download and install R, follow the
steps below:
1. Open The Comprehensive R Archive Network
(CRAN), which is the official R website.
2. In the upper part of the screen, find the section
Download and Install R.
3. Click the link corresponding to your operating
system.
4. Select the latest release.
5. Open the downloaded file and follow simple
installation instructions leaving default options
everywhere.
 To download and install RStudio, follow these
steps:
1. Open the download page of the official RStudio
website.
2. Scroll down to the download buttons for RStudio
Desktop:
3. Click DOWNLOAD RSTUDIO DESKTOP.
4. Click DOWNLOAD under RStudio Desktop:

5. You'll see that your operating system is


automatically identified. Press the big button to
download the latest release of RStudio for your
operating system:
6. Open the downloaded file and follow simple
installation instructions opting for defaults everywhere.
How to Use R Studio
 RStudio Interface

Opening RStudio will automatically launch R software.


The platform interface looks as follows:
Roughly, we can divide the working window into three
areas:
 Left area: includes the tabs Console, Terminal,
and Background Jobs
 Top-right area: includes the
tabs Environment, History, Connections,
and Tutorial
 Bottom-right area: includes the
tabs Files, Plots, Packages, Help, Viewer,
and Presentation

Let's take a closer look at the essential tabs.


 Console
On this tab, we first see the information about the R
version in use and also some basic commands to try. At
the end of those descriptions, we can type our R code,
press Enter, and get the result below the code line
(e.g., try running 2*2 and see what happens). Virtually,
we can do here anything we would do in any other R
program, for example:
 Installing and loading R packages
 Performing simple or complex mathematical
operations
 Assigning the result of an operation to a variable
 Importing data
 Creating common types of R objects, like vectors,
matrices, or dataframes
 Exploring data
 Statistical analysis
 Building data visualizations

 Environment
Whenever we define a new or re-assign an existing
variable in RStudio, it's stored as an object in the
workspace and gets displayed, together with its value,
on the Environment tab in the top-right area of the
RStudio window. Try running greeting <- "Hello,
World!" in the console and see what happens on
the Environment tab.
In the example below, we created two variables in the
console: greeting <- "Hello, World!" and my_vector <-
c(1, 2, 3, 4). Note how they are displayed on
the Environment tab:

We can change the way of displaying our variables


from List to Grid in the top-right corner of the tab, as
follows:
 Other important tabs
 Terminal – to run commands from the terminal

 History – to track the history of all the operations

performed during the current RStudio session

 Files – to see the structure of the working folder,

reset the working folder, navigate between folders,

etc.

 Plots – to preview and export created data

visualizations

 Packages – to check what packages were loaded

and load or unload packages (by switching on/off

the box to the left of a package name)


6 Inbuilt Data Structures in R
with practical examples
In R, most of the time you will be dealing with collections of data and not
singular elements. We hold collections of data in data structures.

Data structures are objects in R that provide a method to arrange data in


the desired format.

In this article, we will take a look at the data structures in R. We will learn
what they are, and what are their usess. We will also explore a few features
and functions of these structures.

Let’s start with understanding the basics of data structures.

Introduction to Data
Structures in R
R has six types of basic data structures. We can organize these data
structures according to their dimensions(1d, 2d, nd). We can also
classify them as homogeneous or heterogeneous (can their
contents be of different types or not).
Homogeneous data structures are ones that can only store a single type of
data (numeric, integer, character, etc.).

Heterogeneous data structures are ones that can store more than one type
of data at the same time.

R does not have 0 dimensional or scalar type. Variables containing single


values are vectors of length 1.

We have discussed every concept of R data types in our previous


article, now we are going to understand R data structures in detail.
R has the following basic data structures:

1. Vector
2. List
3. Matrix
4. Data frame
5. Array
6. Factor

1. Vectors
Vectors are single-dimensional, homogeneous data structures. To
create a vector, use the c() function.
For example:
> vec <- c(1,2,3) # creates a vector named vec
> vec
Output:
[1] 1 2 3
The assign() function is another way to create a vector.

For example:
> assign("vec2", c(4,5,6))
> vec2

Output:
[1] 4 5 6

Vectors can hold values of a single data type. Thus, they can be numeric,
logical, character, integer or complex vectors.
For example:
> numeric_vec <- c(1,2,3,4,5)
> integer_vec <- c(1L,2L,3L,4L,5L)
> logical_vec <- c(TRUE, TRUE, FALSE, FALSE,
FALSE)
> complex_vec <- c(12+2i, 3i, 4+1i, 5+12i, 6i)
> character_vec <- c("techvidvan", "this", "is", "a",
"character vector")
> numeric_vec
> integer_vec
> logical_vec
> complex_vec
> character_vec
Output:
[1] 1 2 3 4 5

[1] 1 2 3 4 5

[1] TRUE FALSE TRUE FALSE FALSE

[1] 12+ 2i 0+ 3i 4+ 1i 5+12i 0+ 6i

[1] “techvidvan” “this”


[3] “is” “a”
[5] “character vector”
2. Lists
Lists are heterogeneous data structures. They are very
similar to vectors except they can store data of different types.
To create a list, we use the list() function.
For example
> test_list <- list(1, "hello", c(2,3,1), FALSE, 3+4i, 6L)
> test_list
Output:
[[1]]
[1] 1
[[2]]
[1] “hello”
[[3]]
[1] 2 3 1
[[4]]
[1] FALSE
[[5]]
[1] 3+4i
[[6]]
[1] 6

Lists are often called “recursive vectors” as you can store


a list inside another list.
Example:
> test_list2<-list(list(1,"a",TRUE), list("b",45L,"c"),
list(1,2))
> str(test_list2) #shows the structure of an object
Output:
List of 3
$ :List of 3
..$ : num 1
..$ : chr “a”
..$ : logi TRUE
$ :List of 3
..$ : chr “b”
..$ : int 45
..$ : chr “c”
$ :List of 2
..$ : num 1
..$ : num 2

3. Matrix
Matrices are two-dimensional, homogeneous data
structures. This means that all values in a matrix have to be of
the same type. Coercion takes place if there is more than one
data type. They have rows and columns.
By default, matrices are in column-wise order. The basic
syntax to create a matrix is:
>matrix( data, nrow, ncol, byrow, dimnames)
Where data is the input values in the matrix given as a vector,
nrow is the number of rows,
ncol is the number of columns,
byrow is a logical which tells the function to arrange the matrix row-wise,
by default it is set to FALSE,
dimnames is a list of the names of the rows/columns created.
The following code will create a matrix with 3 rows and values 1 to 9 in a
column-wise order.

For example:
> test_matrix1 <- matrix(c(1:9), ncol = 3)
> test_matrix1

Output:
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

> rownames <- c("row1", "row2", "row3")


> colnames <- c("col1", "col2", "col3")
> test_matrix2 <- matrix(c(1:9), ncol = 3, dimnames =
list(rownames, colnames))
> test_matrix2
Output:
col1 col2 col3
row1 1 4 7
row2 2 5 8
row3 3 6 9

4. Data Frames
Data frames are two-dimensional,
heterogeneous data structures. They are lists of vectors of
equal lengths. Data frames have the following constraints
placed upon them:
1. A data-frame must have column-names and
each row should have a unique name.
2. Each column should have the same number of
items.
3. Each item in a single column should be of the same
type.
4. Different columns can have different data types.
To create a data frame, use the data.frames() function.
For example:
> student_id <- c(1:5)
> student_name <- c("raj", "jacob", "iqbal", "shawn",
"hitesh")
> student_rank <- c("third", "fifth", "second", "fourth",
"first")
> student.data <- data.frame(student_id ,
student_name, student_rank)
> student.data

Output:
student_id student_name student_rank
1 1 raj third
2 2 jacob fifth
3 3 iqbal second
4 4 shawn fourth
5 5 hitesh first

5. Arrays
Arrays are three dimensional, homogeneous data
structures. They are collections of matrices stacked one on top
of the other in layers.
You can create an array using the array() function. The
following is the syntax of it:
Array_name = array(data,dim,dimnames)
Where array_name is the name of the array,
data is the data that is filled inside the array,
dim is a vector containing the dimensions of the array,
and dimnames is a list containing the names of the rows,
columns, and matrices inside the array.
Here is an example of the array() function:
> arr1 <- array(c(1:18),dim=c(2,3,3))
> arr1
Output:
, , 1[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6, , 2[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12, , 3[,1] [,2] [,3]
[1,] 13 15 17
[2,] 14 16 18
6. Factors
Factors are vectors that can only store predefined values. They
are useful for storing categorical data. Factors have two
attributes:
 Class – which has a value of “factor”, it makes it
behave differently than a normal vector.
 Levels – which is the set of allowed values
You can create a factor using the factor() function.
For example:
> fac <- factor(c("a", "b", "a", "b", "b"))
> fac
Output:
[1] a b a b b
Levels: a b

You might also like