Professional Documents
Culture Documents
[1] 4
👋 Welcome to Notion!
Here are the basics:
Tap the + above your keyboard to add content — headers, sub pages, etc.
Highlight text and use the bar above your keyboard to format
Untitled
History/Introduction
R was developed by Rhoss Jhaka and Robert Gentleman 1993.
Introduction to R 1
It is case sensitive
R can carry out basic arithmetic operations like “+”, “-”, “*”, “^”, “/”, log(..),
cos(..), sin(..), exp(..). He ce it can be used as a simple calculator.
1. ?function name
2. help(function name)
Where function name denotes the name of any function you want to know about.
To quit an R session
>q()
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.
2. ls()
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
Introduction to R 3
Eg:- M1 ✓
1M ×
Eg:- M 1 ×
M1 ✓
Eg of naming:-
>x←c(1,2,3) press enter
>x
[1] 3 2 1
Introduction to R 4
various graphs
Pakages
Bundle of functions
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)
Vectors
Introduction to R 5
1-Dimensional Matrix/object
Numeric
Character
Logical
Eg:- >x←c(3,2,1)
[1] 3 2 1
💡 Here [1] is the index of the first number. It shows the position held by the first
number.
[1] 6 4 2
Introduction to R 6
>y←c(2, 2, 2) ;press enter
>y ;press enter
[1] 2 2 2
[1] 5 4 3
>x+2 →internally 2 is recycled/repeated 3
times
[1] 5 4 3
>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
>m←c(3,10,3,10,3,10)
>x+m
[1] 6 12 4 15 10 16
Eg:-
>x←3.2
Introduction to R 7
>x←c(3.2)
[1] 3.2
Eg:- >floor(3.2)
[1] 3
ceiling(…..) — it will make the number into the nearest large number
[1] 4
Eg:- >s←-6.7
>floor(s)
[1] -7
>ceiling(s)
[1] -6
Eg:- >round(3.7)
Eg:- >trunc(3.7)
[1] 3
Vector of characters/string:-
Introduction to R 8
Although single inverted commas could be used but always use double
Eg:- >y←c(”zoya”,”Alina”,”Vani”)
Read 8 items
1. >x→c
Introduction to R 9
2. >3:9
[1] 3 4 5 6 7 8 9
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.
>seq(from=10,to=2,by=-2)
>x←3.6
>x←c(3,4,5,6)
>sqrt(x)
[1]
Introduction to R 10
>x^2 (for square)
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)
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)
>sum(z[c(3,5)])
[1] 10
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)
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
>sum(w>5)
[1] 2
[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
>w[w≥5]
[1] 6 7 5
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
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
>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