You are on page 1of 17

Introduction to R

[1] 4
👋 Welcome to Notion!
Here are the basics:

Tap anywhere and start typing

Tap the + above your keyboard to add content — headers, sub pages, etc.

Example sub page

Highlight text and use the bar above your keyboard to format

This bar scrolls from left to right

Tap and hold this line, then drag

Tap the ☰ button to open sidebar

👉Have a question? Tap Help & feedback in the sidebar.

Untitled

History/Introduction
R was developed by Rhoss Jhaka and Robert Gentleman 1993.

It was made an open source in 1995.

It was developed on lines of ‘’S”.

It is command driven i.e. dependent of functions completely (hence has a


widespread scope as we can write any command as per our necessity an execute
it) not menu based.

Introduction to R 1
It is case sensitive

Can be downloaded from CRAN website.

R can carry out basic arithmetic operations like “+”, “-”, “*”, “^”, “/”, log(..),
cos(..), sin(..), exp(..). He ce it can be used as a simple calculator.

RStudio is an IDE(integrated development environment) for R

Ways to get to know about a function

1. ?function name

2. help(function name)

Where function name denotes the name of any function you want to know about.

Way to get examples on a particular function


example(function name)

This is a suppliment of help function.

To quit an R session
>q()

Note:- The symbol > is known as command prompt

Workspace
The collection of all objects that we have created and are already stored.

Introduction to R 2
We do not save our workspace before quitting the session, we just copy-paste our
commands to any word file

If we say yes to save workspace in the dialogue box, it gets stored in .RData file
type.

All the command that we have entered for that object is saved in .Rhistory

Objects
Whatever we create and manipulate in R are known as objects in R.

Ways to review/print the created objects


1. object()

2. ls()

We can review our objects only if we work on the same directory

Kind of Objects defined in R


1. Vector

2. Matrix

a. Array

3. Dataframe

4. List

Naming of Objects in R
When we assign the result of any calculation to a object, we can store the calculation

←- assignment operator

Rules of naming objects


1. Names should start with character only, it cannot start with numeric stuff.

Introduction to R 3
Eg:- M1 ✓
1M ×

2. White space is not allowed

Eg:- M 1 ×

M1 ✓
Eg of naming:-
>x←c(1,2,3) press enter

>x

[1] 3 2 1

Ways to remove objects from memory of R


>rm(…,…,…)
The round brackets contain the name of the objects to be removed. Multiple objects can
be removed at a time by seperating through commas.

To see some predefined objects in R


1. >month.name

2. >month.abb →we get abbreviated names


of months. Eg:- Jan, Feb,
etc.

3. >LETTERS →we get all caps English


alphabets

4. >letters →we get all small English


alphabets.

5. >colors →we get names of all colours.

6. >demo(graphics) →we get a


demonstration of

Introduction to R 4
various graphs

Pakages
Bundle of functions

Ways to install pakages


>install.pakages(”………”)

Ways to load the Library of the pakage

1. >library(……..)

2. >require(…….)

Note:- the name of the pakage has be mentioned with the round brackets

Eg:-

>install.pakages(”arm”)

>library(arm)
OR

>require(arm)

Where arm is the name of the package

TYPES OF DATA STRUCTURES IN R

Vectors

Introduction to R 5
1-Dimensional Matrix/object

It is a sequence of same data type.

Basic data types are:-

Numeric

Character

Logical

It works on vector as a 1 single object.

Eg:- >x←c(3,2,1)

Here (3,2,1) is treated as a single item/object.

Creating vector using function ‘c’


All arithmetic operations can be carried out.

Some examples include

>x←c(3,2,1) ;press enter

>x ;press enter

[1] 3 2 1

💡 Here [1] is the index of the first number. It shows the position held by the first
number.

💡 ;press enter is a comment. Should not be written on console but to be


done

>x*2 ;press enter

[1] 6 4 2

Introduction to R 6
>y←c(2, 2, 2) ;press enter
>y ;press enter

[1] 2 2 2

>x+y → element wise addition

[1] 5 4 3
>x+2 →internally 2 is recycled/repeated 3
times

[1] 5 4 3

Addition when the length of vector is not equal:-

>x←c(3, 2, 1, 5, 7, 6)
>z←c(3,10)
>x+z ;press enter through repetition

[1] 6 12 4 15 10 16

💡 Repetition can only occur in case of unequal vectors if one is a multiple of


other

The above can be executed as follows as well

>m←c(3,10,3,10,3,10)
>x+m

[1] 6 12 4 15 10 16

With Single value:-

R does not recognises a single value as a scalar, it recognises it as a vector of


Single element. There is no concept of scalars

Eg:-

>x←3.2

Introduction to R 7
>x←c(3.2)
[1] 3.2

Some function on single element vector


floor(……..) — it makes the number containing with the round brackets into nearest
small number.

Eg:- >floor(3.2)

[1] 3

ceiling(…..) — it will make the number into the nearest large number

Eg:- >ceiling (3.2)

[1] 4

Eg:- >s←-6.7

>floor(s)
[1] -7
>ceiling(s)

[1] -6

round(….) — for rounding off values

Eg:- >round(3.7)

trunc(….) — drops the decimal part

Eg:- >trunc(3.7)

[1] 3

Vector of characters/string:-

No arithmetic operations possible

The elements of the argument should be within double inverted commas

Introduction to R 8
Although single inverted commas could be used but always use double

Eg:- >y←c(”zoya”,”Alina”,”Vani”)

To create vector using ‘scan’ function


It also tells the length of vector in addition to printing the vector

Eg:- >x←scan() ;press enter

1:3 1 2 {;press enter to go to another


line in which any number of
vectors can be added
irrespective of the number
of vectors in the previous
line}

4:7 11 3 0 1 {;press enter twice if


you are done making
entries and wish to
see the result }

Read 8 items

💡 length(x) — To cross check the number of elements ina vector.

To create a vector using “:”


“:” adds 1 to each of the pre
Eg:- create a vector 3,4,5,6,7,8,9

1. >x→c

Introduction to R 9
2. >3:9

[1] 3 4 5 6 7 8 9

To create a vector using “seq”

Create a vector 3,5,7,9,11,13


>seq(from=3,to 13,by=12)
[1] 3 5 7 9 11 13

Create a vector from 3 to 13 such that there are 10 elements between these vector
>seq(from=3,to=13,length=10)

>seq(to=13,from=3,length =10)
Both of the above shall give the same result.

Remember the argument then order of argument shall not matter.

Create a vector with elements 10, 8,6,4,2

>seq(from=10,to=2,by=-2)

>x←3.6

>sqrt(x) (for square root)


[1]

>x←c(3,4,5,6)
>sqrt(x)

[1]

Introduction to R 10
>x^2 (for square)

>x^3 (for cube)


>x^1/3 (for cube root)

>log(x) (will give to the base e)


>exp(x)
>sin(x) (shall give ans in radians)

>log(x,base=3) (will give the value with the specified base)

To add additional information


>x←c(34,65,87)
>names(x)←c(“Zoya”,“Alina”,“Alka”)

[1] Zoya Alina Alka


34. 65. 87
>x (press enter)

To create a vector which is combination of numbers and strings

1.

>paste(c(“Zoya”,Alina”,”Alka”),c(34,65,87),sep=“-“)

sep=“” {will give no space between the name and the numbers assigned}
sep=“.” {will give . between the name and the numbers assigned}

2.

>students←c(“Zoya”,Latina”,”Alka”)
>marks←c(34, 45, 56)

>paste(student,marks,sep=“ “) {we will a space between the name and marks}

Introduction to R 11
To add the elements of a vector or to add a number of vector
>z←c(6,4,7,8)

💡 [ ] →extraction operator

[ ] →extraction operator
>z[1]+z[2]+z[3]+z[4]

[1] 10
>z[1]+z[2]

[1] 3
>sum(z)
[1] 10

>z←c(2,3,4,5,6)

Extract the 3rd elements


>z[3] {this is an index vector with 1 element}
[1] 4

Extract the 3rd and last elements


>z[c(3,5)]
[1] 4 6

To find the sum of few elements of a vector

>sum(z[c(3,5)])
[1] 10

Find the mean of all elements of a vector


>sum(z)/5

Introduction to R 12
OR

>sum(z)/length(z)

💡 >mode(z) will give the type of vector z is. It does not give the modal value

If we have a missing value data and we want to know which of the observation in
missing from the data
Use the function is.na()

>z←c(3,4,5,NA,9)

>is.na(z) {function to find which entry is missing; we get a logical vector}


[1] False False False True False

How many of the observations are missing


>sum(is.na(z))
[1] 1

Through assignment operator


>p←is.na(z)
>sum(p)
[1] 1

Now if we want to find the sum of the rest of the vector after dropping the NA
vector
>sum(z,na.rm=T)

Introduction to R 13
[1] 21

to find the mean of the rest of the vector after dropping the NA vector
>sum(z,na.rm=T)/4
[1] 5.25

OR

💡 We cannot use the length vector in case of NA vector with mean as it gets
divided by the entire no of observation.

>mean(z,na.rm=T)
[1] 5.25

To get to know which of the observation in the vector are greater than a particular
Number

>w←c(1,6,7,4,5)
>w>5
[1] False True True False False

No of vector greater than a particular number

>sum(w>5)
[1] 2

What all observation are greater than a particular elements


>w[w>5]

[1] 6 7

Introduction to R 14
Mean of observatIon which are greater than a particular number
>p←w[w>5]
>mean(p)
[1] 6.5

OR
>mean(w[w>5])
[1] 6.5

What all observation are greater than or equal to a particular elements

>w[w≥5]
[1] 6 7 5

What all observation are not equal a particular elements


>w[w≠5]

What all observation are equal to a particular elements


>w==5
[1] FALSE FALSE FALSE FALSE TRUE

To get the number of observation equal to a particular element

>sum(w==5) {no of elements that are equal to 5)


[1] 1

Find the log of all value of a vector and add them


>sum(log(w))

Also can use assignment operator

Introduction to R 15
To get to know which of the observation in the vector are greater than and less
than the given numbers
>w>5&w<9

[1] False True False False

No of vector greater than and less than the given numbers


>sum(w>5&w<9)
[1] 2

What all observation are greater than and less than the given elements
>w[w>5&w<9]
[1] 6 7

To get to know which of the observation in the vector are greater than OR less
than the given numbers
>w>5|w<9
[1] True False

What all observation( mention the observation) are greater than OR less than the
given numbers
>w[w>5|w<9]
[1] 1 2 5 6 7

To reduce the number upto 1 places of decimal

>p←c(3.13,9.99,1.3,1.87)
>round(p,digits=1)
[1] 3.1 10.0 1.3 1.9

Introduction to R 16
Introduction to R 17

You might also like