0% found this document useful (0 votes)
3 views

Introduction R Programming Lab_231009_075137

The document provides an introduction to R programming and its installation process, including downloading R and RStudio on Windows. It outlines basic commands, data types, and arithmetic operations in R, emphasizing its use for data analysis and visualization. Additionally, it explains how to install and load R packages, highlighting the importance of RStudio as an integrated development environment.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Introduction R Programming Lab_231009_075137

The document provides an introduction to R programming and its installation process, including downloading R and RStudio on Windows. It outlines basic commands, data types, and arithmetic operations in R, emphasizing its use for data analysis and visualization. Additionally, it explains how to install and load R packages, highlighting the importance of RStudio as an integrated development environment.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

5SEM_BCA_R PROGRAMMING LAB COURSE CODE: DSC14-LAB

Introduction to R programming Lab


What is R?
R is a free, open source statistical programming language. It is useful for data cleaning, analysis, and visualization. R is an
interpreted language, not a compiled one. This means that you type something into R and it does what you tell it. It is both
a command line software and a programming environment. It is an extensible, open-source language and computing
environment for Windows, Macintosh, UNIX, and Linux platforms, which allows for the user to freely distribute, study,
change, and improve the software. It is basically a free, super big, and complex calculator. You will be using R to
accomplish all data analysis tasks in this class.

Getting R
R can be downloaded from one of the “CRAN” (Comprehensive R Archive Network) sites. In the US, the main site is
at http://cran.us.r-project.org/. Look in the “Download and Install R” area. Click on the appropriate link based on your
operating system.(preferably Windows)

On Windows
1. On the “R for Windows” page, click on the “base” link, which should take you to the “R-4.2.2 for Windows
(32/64 bit)” page
2. On this page, click “Download R 4.2.2 for Windows”, and save the exe file to your hard disk when prompted.
Saving to the desktop is fine.
3. To begin the installation, double-click on the downloaded file. Don’t be alarmed if you get unknown publisher
type warnings. Window’s User Account Control will also worry about an unidentified program wanting access to
your computer. Click on “Run”.
4. Select the proposed options in each part of the install dialog. When the “Select Components” screen appears, just
accept the standard choices
Note: Depending on the age of your computer and version of Windows, you may be running either a “32-bit” or “64-bit”
version of the Windows operating system. If you have the 64-bit version (most likely), R will install the appropriate
version (R x64 3.5.2) and will also (for backwards compatibility) install the 32-bit version (R i386 3.5.2). You can run
either, but you will probably just want to run the 64-bit version.

What is RStudio?
If you click on the R program you just downloaded, you will find a very basic user interface. For example, below is what I
get on a Windows
R-TERMINAL

DEPARTMENT OF BCA, SESHADRIPURAM COLLEGE, TUMAKURU-5 1


5SEM_BCA_R PROGRAMMING LAB COURSE CODE: DSC14-LAB

We will not use R’s direct interface to run analyses. Instead, we will use the program RStudio.
RStudio gives you a true integrated development environment (IDE), where you can write code in a window, see results in
other windows, see locations of files, see objects you’ve created, and so on. To clarify which is which: R is the name of
the programming language itself and RStudio is a convenient interface

R Studio Window

Installation of R-Studio on windows:

Step – 1: With R-base installed, let’s move on to installing RStudio. To begin, goto
download RStudio and click on the download button for RStudio desktop.

DEPARTMENT OF BCA, SESHADRIPURAM COLLEGE, TUMAKURU-5 2


5SEM_BCA_R PROGRAMMING LAB COURSE CODE: DSC14-LAB

Step – 2: Click on the link for the windows version of RStudio and save the .exe file.

Step – 3: Run the .exe and follow the installation instructions.

3. Click Next on the welcome window.

Enter/browse the path to the installation folder and click Next to proceed.

DEPARTMENT OF BCA, SESHADRIPURAM COLLEGE, TUMAKURU-5 3


5SEM_BCA_R PROGRAMMING LAB COURSE CODE: DSC14-LAB

Select the folder for the start menu shortcut or click on do not create shortcuts and thenclick
Next.

Wait for the installation process to complete.

DEPARTMENT OF BCA, SESHADRIPURAM COLLEGE, TUMAKURU-5 4


5SEM_BCA_R PROGRAMMING LAB COURSE CODE: DSC14-LAB

Click Finish to end the installation.

How to Install the R Packages:-


In RStudio, if you require a particular library, then you can go through the following
instructions:

 First, run R Studio.


 After clicking on the packages tab, click on install. The following dialog box will appear.
 In the Install Packages dialog, write the package name you want to install under
the Packages field and then click install. This will install the package you
searched for or give you a list of matching packages based on your package text.

Installing Packages:-
The most common place to get packages from is CRAN. To install packages from CRAN you
use install.packages("package name"). For instance, if you want to install the ggplot2
package, which is a very popular visualization package, you would type the following in the
console:-
Syntax:-
# install package from CRAN
install.packages("ggplo2")
Loading Packages:-
Once the package is downloaded to your computer you can access the functions and
resources provided by the package in two different ways:
# load the package to use in the current R session
library(packagename)

DEPARTMENT OF BCA, SESHADRIPURAM COLLEGE, TUMAKURU-5 5


5SEM_BCA_R PROGRAMMING LAB COURSE CODE: DSC14-LAB

Basic Commands in R
Assignment Operators:-
The first operator you’ll run into is the assignment operator. The assignment operator is usedto assign a value

For instance we can assign the value 3 to the variable x using the <- assignment operator.
# assignment
x <- 3
Interestingly, R actually allows for five assignment operators:
# leftward assignment
x <- value
x = value
x <<- value
Rightward assignment
value -> x
value ->> x
The original assignment operator in R was <- and has continued to be the preferred among R
users. The = assignment operator was added in 2001 primarily because it is the accepted
assignment operator in many other languages and beginners to R coming from other
languages were so prone to use it.
The operators <<- is normally only used in functions which we will not get into the details.
Evaluation
We can then evaluate the variable by simply typing x at the command line which will return
the value of x. Note that prior to the value returned you’ll see ## [1] in the command line.
This simply implies that the output returned is the first output. Note that you can type any
comments in your code by preceding the comment with the hash tag (#) symbol. Any values,
symbols, and texts following # will not be evaluated.
# evaluation
x
## [1] 3

Case Sensitivity
Lastly, note that R is a case sensitive programming language. Meaning all variables,
functions, and objects must be called by their exact spelling:
x <- 1
y <- 3
z <- 4
x*y*z
## [1] 12
x*Y*z
## Error in eval(expr, envir, enclos): object 'Y' not found

DEPARTMENT OF BCA, SESHADRIPURAM COLLEGE, TUMAKURU-5 6


5SEM_BCA_R PROGRAMMING LAB COURSE CODE: DSC14-LAB

Basic Arithmetic
At its most basic function R can be used as a calculator. When applying basic arithmetic, the
PEMDAS order of operations applies: parentheses first followed by exponentiation,
multiplication and division, and final addition and subtraction.

8+9/5^2
## [1] 8.36

8 + 9 / (5 ^ 2)
## [1] 8.36
8 + (9 / 5) ^ 2
## [1] 11.24
(8 + 9) / 5 ^ 2
## [1] 0.68
By default R will display seven digits but this can be changed using options() as previously
outlined.
1/7
## [1] 0.1428571
options(digits = 3)
1/7
## [1] 0.143
pi
## [1] 3.141592654
options(digits = 22)
pi
## [1] 3.141592653589793115998
We can also perform integer divide (%/%) and modulo (%%) functions. The integer divide
function will give the integer part of a fraction while the modulo will provide the remainder.
42 / 4 # regular division
## [1] 10.5
42 %/% 4 # integer division
## [1] 10
42 %% 4 # modulo (remainder)
## [1]

Some Mathematical Functions


There are many built-in functions to be aware of. These include but are not limited to the
following. Go ahead and run this code in your console.
x <- 10
abs(x) # absolute value sqrt(x) # square root
exp(x) # exponential transformation
cos(x) # cosine and other trigonometric functions

DEPARTMENT OF BCA, SESHADRIPURAM COLLEGE, TUMAKURU-5 7


5SEM_BCA_R PROGRAMMING LAB COURSE CODE: DSC14-LAB

R Data Types
R has five basic data types or “atomic” classes of objects:
• character
• numeric (real numbers)
• integer
• complex
• logical (True/False)

Let’s now explore what R can do. R is really just a big fancy calculator. For example, type in the following
mathematical expression in the R console (left window)
HIDE

1+1
## [1] 2

Note that spacing does not matter: 1+1 will generate the same answer as 1 + 1. Can you say hello to the
world?
HIDE

hello world
## Error: <text>:1:7: unexpected symbol
## 1: hello world
## ^

Nope. What is the problem here? We need to put quotes around it.
HIDE

"hello world"
## [1] "hello world"

“hello world” is a character and R recognizes characters only if there are quotes around it. This brings us to the
topic of basic data types in R. There are four basic data types in R: character, logical, numeric, and factors
(there are two others - complex and raw - but we won’t cover them because they are rarely used in practice).

Characters
Characters are used to represent words or letters in R. We saw this above with “hello world”. Character values
are also known as strings. You might think that the value "1" is a number. Well, with quotes around, it isn’t!
Anything with quotes will be interpreted as a character. No ifs, ands or buts about it.

DEPARTMENT OF BCA, SESHADRIPURAM COLLEGE, TUMAKURU-5 8


5SEM_BCA_R PROGRAMMING LAB COURSE CODE: DSC14-LAB

Logical or Boolean
A logical takes on two values: FALSE or TRUE. Logicals are usually constructed with comparison operators,
which we’ll go through more carefully in Lab 2. Think of a logical as the answer to a question like “Is this
value greater than (lower than/equal to) this other value?” The answer will be either TRUE or FALSE. TRUE
and FALSE are logical values in R. For example, typing in the following

3>2
## [1] TRUE

Gives you a true. What about the following?

"jacob" == "catherine"
## [1] FALSE

Numeric
Numerics are separated into two types: integer and double. The distinction between integers and doubles is
usually not important. R treats numerics as doubles by default because it is a less restrictive data type. You can
do any mathematical operation on numeric values. We added one and one above. We can also multiply using
the * operator.

2*3
## [1] 6

Divide

4/2
## [1] 2

And even take the logarithm!

log(1)
## [1] 0

log(0)
## [1] -Inf

Uh oh. What is -Inf? Well, you can’t take the logarithm of 0, so R is telling you that you’re getting a non
numeric value in return. The value -Inf is another type of value type that you can get in R

DEPARTMENT OF BCA, SESHADRIPURAM COLLEGE, TUMAKURU-5 9

You might also like