SlideShare uma empresa Scribd logo
1 de 11
Learning R
Kamal Gupta Roy
Last Edited on Oct 17, 2021
Instructions / Agenda
1. Create Simple Arithmetic Operations. [+, -, *, /, ˆ, %%]
2. Create Logical Operation [<, >, < 1, > 1]
3. Built-in functions [sqrt, log, exp, floor, round, ceiling, abs]
4. Assignment of values to variables [a <- 2, b <- 3]
5. Retest arithmetic operations using variables
6. Saving arithmetic operations of two variables into a new variable
7. Introdution to Vector, vec <- c(23,4,12,78)
8. Finding values in the vectors using positions
9. Arithmetic Operations on vectors: addition, subtraction, multiplication
10. In built functions: mean(), median(), max(), min(), sort(), var(), sd(), cor, which.min and
which.max
11. Numeric Vector, Character Vector, combining the two, class function
12. creating a vector using function seq [seq(10,70,by=10), seq(10,70,length=10)], help on seq func-
tion
13. Directory functions [getwd(), setwd()]
14. Read a csv file; split columns into vectors and do some basic formulation
15. Exercises
Simple Arithmetic Operations
5 + 2
## [1] 7
5 * 2
## [1] 10
1
5 - 2
## [1] 3
5 / 2
## [1] 2.5
5 %% 2
## [1] 1
5 ˆ 2
## [1] 25
Logical Operations
2<1
## [1] FALSE
1<2
## [1] TRUE
(2<1)*1
## [1] 0
(1<2)*1
## [1] 1
Built in Functions
sqrt(26)
## [1] 5.09902
log(2)
## [1] 0.6931472
2
exp(2)
## [1] 7.389056
floor(2.467)
## [1] 2
round(2.467,2)
## [1] 2.47
ceiling(3.723)
## [1] 4
abs(-1)
## [1] 1
Assignment of Variables
a <- 2
b <- 7
a + b
## [1] 9
a - b
## [1] -5
a ˆ b
## [1] 128
c <- a ˆ b
c
## [1] 128
Vector Introduction
3
vec <- c(23,4,12,78,4,98,12,34,52,73,59,30)
length(vec)
## [1] 12
vec[7]
## [1] 12
vec[-3]
## [1] 23 4 78 4 98 12 34 52 73 59 30
vec[15]
## [1] NA
vec[2:4]
## [1] 4 12 78
vec[c(5,8)]
## [1] 4 34
Arithmetic Operations on vectors
quiz1 <- c(12,34,27,31)
quiz2 <- c(34,16,22,11)
quiz1 + quiz2
## [1] 46 50 49 42
quiz1 - quiz2
## [1] -22 18 5 20
quiz1/quiz2
## [1] 0.3529412 2.1250000 1.2272727 2.8181818
4
round(quiz1/quiz2,1)
## [1] 0.4 2.1 1.2 2.8
quiz1*quiz2
## [1] 408 544 594 341
In built functions
vec <- c(12,34,27,31,55,11,99,67,45,27)
mean(vec)
## [1] 40.8
median(vec)
## [1] 32.5
max(vec)
## [1] 99
min(vec)
## [1] 11
sort(vec)
## [1] 11 12 27 27 31 34 45 55 67 99
sort(vec,decreasing = TRUE)
## [1] 99 67 55 45 34 31 27 27 12 11
cor(quiz1,quiz2)
## [1] -0.9333819
var(vec)
## [1] 725.9556
5
sd(vec)
## [1] 26.94356
what is marks of a student in quiz1 who scored highest/least in quiz2?
which.min(quiz2)
## [1] 4
i <- which.min(quiz2)
quiz1[i]
## [1] 31
x <- cbind(quiz1,quiz2)
x
## quiz1 quiz2
## [1,] 12 34
## [2,] 34 16
## [3,] 27 22
## [4,] 31 11
cor(x)
## quiz1 quiz2
## quiz1 1.0000000 -0.9333819
## quiz2 -0.9333819 1.0000000
Combining Vectors
numb <- c(1,3,5,7)
chara <- c("A","B","C")
comb <- c(numb,chara)
class(numb)
## [1] "numeric"
class(chara)
## [1] "character"
6
class(comb)
## [1] "character"
Vectors creating from function seq
s1 <- seq(10,70,by=10)
s1
## [1] 10 20 30 40 50 60 70
s2 <- seq(10,70,length=7)
s2
## [1] 10 20 30 40 50 60 70
rand <- c(floor(runif(n = 50, min = 1, max = 10)))
rand
## [1] 8 4 1 2 8 2 9 5 4 2 8 2 7 4 4 3 9 6 8 5 4 7 6 1 5 4 9 6 2 7 3 4 9 7 5 3 4 7
## [39] 1 2 3 3 3 9 9 2 1 9 9 5
#?seq
#help(seq)
Operations on vectors
s1<-seq(1,20, length=5)
s2<-seq(40,60, length=5)
s1 + s2
## [1] 41.00 50.75 60.50 70.25 80.00
s1*s2
## [1] 40.00 258.75 525.00 838.75 1200.00
sum(s1*s2)
## [1] 2862.5
Naming of Vector
7
# Create a card vector as a character vector and a number vector c("Jack","King","Queen","Ace")
card <- c("Jack","King","Queen","Ace")
cardn <- c(11,13,12,14)
card
## [1] "Jack" "King" "Queen" "Ace"
cardn
## [1] 11 13 12 14
#Giving a name to the cardn vector
names(cardn) <- card
#Option 2:
cardn <- c("Jack" = 11,"King" = 13,"Queen" = 12,"Ace" = 14)
#Option 3:
cardn <- c(Jack = 11, King = 13, Queen= 12, Ace = 14)
Directory Details
#### workspace
ls()
## [1] "a" "b" "c" "card" "cardn" "chara" "comb" "i" "numb"
## [10] "quiz1" "quiz2" "rand" "s1" "s2" "vec" "x"
#To know what is the default working directory
getwd()
## [1] "C:/Users/Debzitt/Desktop"
# Setting a Working Directory using setwd()
#setwd(C:/Users/Admin/)
getwd()
## [1] "C:/Users/Debzitt/Desktop"
Read a csv file
df <- read.csv("veg_prices.csv")
df
8
## ITEM QUANTITY UNIT_PRICE
## 1 Bangalore Tomato (Bangalore Thakkali) 3 17.44
## 2 Beans (Beans) 5 20.14
## 3 Beetroot (Beetroot) 5 14.05
## 4 Bitter Gourd (Pavakkai) 5 18.69
## 5 Bottle Gourd (Suraikai) 5 10.99
## 6 Brinjal (Kathirikkai) 5 11.99
## 7 Broad Beans (Avarakkai) 3 19.49
Calculate total bill from veg prices by splitting the dataframe into vectors
sum(df$QUANTITY * df$UNIT_PRICE)
## [1] 490.09
Exercises
Exercise 1
There are two sections. Assign random number to number of girls and number of boys in each
section. Calculate:
a. Total number of students in each section
b. Find out which section has more girls, boys and students
Exercise 2
Create a random numeric vector of atleast length of 10. Values should vary between 01-99.
Calculate:
a. what is the 5th value in the vector?
b. What is the last value in the vector?
c. What is the value of 3rd position from last?
d. what is the difference between 8th and 2nd value of the vector?
e. How many numbers are > 50?
f. How many numbers are more than average of the vector?
g. How many numbers are divisible by 3?
9
Exercise 3
Create a vector with marks in the class (atleast 10 numbers, varying between 00-99) all values
should be different.
a. What is the maximum value?
b. What is the minimum value?
c. What is the range?
d. What are the average marks?
e. If maximum value is replaced by 82, what is the new average? Is your average increased or decreased?
f. If another value 82 is added as a part of the vector, what is the new average? Is your average increased
or decreased?
Exercise 4
Calculate the following:
a. Find the sum of first 5 numbers
b. Find the sum of first 100 natural numbers. Use atleast two ways to find the sum
c. Find the difference between sum of numbers between 51-100 and 1-50
Exercise 5
Prisha invests Rs 68,000 at 9% rate per annum for a certain period. Calculate:
a. Total money Prisha gets after 5 years (Based on simple Interest)
b. Total money Prisha gets after 5 years (Based on compound Interest, if compounded annually)
Exercise 6
Aarav invests Rs 89,000 at 7% rate per annum for a two year lock-in period. Interest would be
calculated compounded annually. Aarav needed money after 1 year and 10 months to pay for his
college fees. He goes to the bank for withdrawl. Bank Manager told him that he would be given
money back based on simple interest calculation. Also, Bank would charge a penalty of 10% of
earned Interest.
a. How much money Aarav would get after penalty?
b. How much money he would have got more if he could wait for 2 more months?
10
Exercise 7
Need to solve the quadratic equation.
a. Give the roots of the equation, 2x2
+ 7x + 3 = 0
#finding the maturity amount if 10% interest is compounded every year
#n= time period
new1.function = function(faceValue,rOfInterest,n)
{
marketValue <- faceValue*(1+(rOfInterest/100))^n
print(marketValue)
}
new1.function(10000,10,10)
#finding the maturity amount if 10% is calculated is as simple interest on face value
new2.function = function(faceValue,rOfInterest,n)
{
marketValue <- faceValue + (faceValue*rOfInterest*n)/100
print(marketValue)
}
new2.function(10000,10,10)
11

Mais conteúdo relacionado

Mais procurados

Supply chain management at fed ex
Supply chain management at fed exSupply chain management at fed ex
Supply chain management at fed exSneha Joy
 
Vishal mega mart
Vishal mega martVishal mega mart
Vishal mega martRavi Verma
 
Wal mart stores inc supply chain management
Wal mart stores inc   supply chain managementWal mart stores inc   supply chain management
Wal mart stores inc supply chain managementaliyudhi_h
 
Orient Craft Ltd
Orient Craft LtdOrient Craft Ltd
Orient Craft Ltdsahil
 
Walmart’s value chain
Walmart’s value chainWalmart’s value chain
Walmart’s value chainoumnicht
 
Final examination 2011 class vii
Final examination 2011 class viiFinal examination 2011 class vii
Final examination 2011 class viiAsad Shafat
 
Wal Mart Strategy Analysis
Wal Mart Strategy AnalysisWal Mart Strategy Analysis
Wal Mart Strategy AnalysisMrirfan
 
Basic Probability and statistics in Bangla
Basic Probability and statistics in BanglaBasic Probability and statistics in Bangla
Basic Probability and statistics in BanglaNaimul Arif
 
Walmart Operation Management - A slight Overview
Walmart Operation Management - A slight OverviewWalmart Operation Management - A slight Overview
Walmart Operation Management - A slight OverviewKunal Gawade, CFE
 
Imran Naim CV Final
Imran Naim CV FinalImran Naim CV Final
Imran Naim CV Finalimran naim
 
Dmart marketing strategy
Dmart marketing strategyDmart marketing strategy
Dmart marketing strategyAnuranjanSingh9
 
wal mart case study
wal mart case study wal mart case study
wal mart case study Crystal lee
 
Case Study of Subhiksha
Case Study of SubhikshaCase Study of Subhiksha
Case Study of SubhikshaAnunay Anand
 
Reliance fresh
Reliance freshReliance fresh
Reliance freshBhanu Negi
 

Mais procurados (18)

Supply chain management at fed ex
Supply chain management at fed exSupply chain management at fed ex
Supply chain management at fed ex
 
Vishal mega mart
Vishal mega martVishal mega mart
Vishal mega mart
 
Wal mart stores inc supply chain management
Wal mart stores inc   supply chain managementWal mart stores inc   supply chain management
Wal mart stores inc supply chain management
 
Orient Craft Ltd
Orient Craft LtdOrient Craft Ltd
Orient Craft Ltd
 
Walmart’s value chain
Walmart’s value chainWalmart’s value chain
Walmart’s value chain
 
Supply Chain Management Shangrila Food (PVT) Ltd
Supply Chain Management Shangrila Food (PVT) LtdSupply Chain Management Shangrila Food (PVT) Ltd
Supply Chain Management Shangrila Food (PVT) Ltd
 
Project on Big bazaar
Project on Big bazaarProject on Big bazaar
Project on Big bazaar
 
Final examination 2011 class vii
Final examination 2011 class viiFinal examination 2011 class vii
Final examination 2011 class vii
 
Wal Mart Strategy Analysis
Wal Mart Strategy AnalysisWal Mart Strategy Analysis
Wal Mart Strategy Analysis
 
Basic Probability and statistics in Bangla
Basic Probability and statistics in BanglaBasic Probability and statistics in Bangla
Basic Probability and statistics in Bangla
 
Rokomari.com Slides
Rokomari.com SlidesRokomari.com Slides
Rokomari.com Slides
 
Walmart Operation Management - A slight Overview
Walmart Operation Management - A slight OverviewWalmart Operation Management - A slight Overview
Walmart Operation Management - A slight Overview
 
Imran Naim CV Final
Imran Naim CV FinalImran Naim CV Final
Imran Naim CV Final
 
Arvind mills
Arvind mills Arvind mills
Arvind mills
 
Dmart marketing strategy
Dmart marketing strategyDmart marketing strategy
Dmart marketing strategy
 
wal mart case study
wal mart case study wal mart case study
wal mart case study
 
Case Study of Subhiksha
Case Study of SubhikshaCase Study of Subhiksha
Case Study of Subhiksha
 
Reliance fresh
Reliance freshReliance fresh
Reliance fresh
 

Semelhante a Learning R

NPTEL QUIZ.docx
NPTEL QUIZ.docxNPTEL QUIZ.docx
NPTEL QUIZ.docxGEETHAR59
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2Kevin Chun-Hsien Hsu
 
8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data framesExternalEvents
 
R Activity in Biostatistics
R Activity in BiostatisticsR Activity in Biostatistics
R Activity in BiostatisticsLarry Sultiz
 
This quiz is open book and open notes/tutorialoutlet
This quiz is open book and open notes/tutorialoutletThis quiz is open book and open notes/tutorialoutlet
This quiz is open book and open notes/tutorialoutletBeardmore
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data framesFAO
 
A practical work of matlab
A practical work of matlabA practical work of matlab
A practical work of matlabSalanSD
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmitabeasiswa
 
Introduction of MatLab
Introduction of MatLab Introduction of MatLab
Introduction of MatLab Imran Nawaz
 
MATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdfMATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdfahmed8651
 
50 SDE Interview Questions.pdf
50 SDE Interview Questions.pdf50 SDE Interview Questions.pdf
50 SDE Interview Questions.pdfHeyCoach
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlabTUOS-Sam
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentationManchireddy Reddy
 
Matrix algebra in_r
Matrix algebra in_rMatrix algebra in_r
Matrix algebra in_rRazzaqe
 

Semelhante a Learning R (20)

NPTEL QUIZ.docx
NPTEL QUIZ.docxNPTEL QUIZ.docx
NPTEL QUIZ.docx
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data frames
 
R Activity in Biostatistics
R Activity in BiostatisticsR Activity in Biostatistics
R Activity in Biostatistics
 
R Programming Homework Help
R Programming Homework HelpR Programming Homework Help
R Programming Homework Help
 
Vectormaths and Matrix in R.pptx
Vectormaths and Matrix in R.pptxVectormaths and Matrix in R.pptx
Vectormaths and Matrix in R.pptx
 
This quiz is open book and open notes/tutorialoutlet
This quiz is open book and open notes/tutorialoutletThis quiz is open book and open notes/tutorialoutlet
This quiz is open book and open notes/tutorialoutlet
 
6. R data structures
6. R data structures6. R data structures
6. R data structures
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data frames
 
A practical work of matlab
A practical work of matlabA practical work of matlab
A practical work of matlab
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmita
 
Introduction of MatLab
Introduction of MatLab Introduction of MatLab
Introduction of MatLab
 
MATLAB ARRAYS
MATLAB ARRAYSMATLAB ARRAYS
MATLAB ARRAYS
 
MATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdfMATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdf
 
50 SDE Interview Questions.pdf
50 SDE Interview Questions.pdf50 SDE Interview Questions.pdf
50 SDE Interview Questions.pdf
 
coefficient variation
coefficient variationcoefficient variation
coefficient variation
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlab
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentation
 
Matrix algebra in_r
Matrix algebra in_rMatrix algebra in_r
Matrix algebra in_r
 

Mais de Kamal Gupta Roy

Mais de Kamal Gupta Roy (6)

Decision_tree.pdf
Decision_tree.pdfDecision_tree.pdf
Decision_tree.pdf
 
Text analytics
Text analyticsText analytics
Text analytics
 
Media savvy for data news
Media savvy for data newsMedia savvy for data news
Media savvy for data news
 
Graphics in R
Graphics in RGraphics in R
Graphics in R
 
Rdplyr+pdf
Rdplyr+pdfRdplyr+pdf
Rdplyr+pdf
 
Knn Algorithm
Knn AlgorithmKnn Algorithm
Knn Algorithm
 

Último

Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...amitlee9823
 
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men 🔝Bangalore🔝 Esc...
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men  🔝Bangalore🔝   Esc...➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men  🔝Bangalore🔝   Esc...
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men 🔝Bangalore🔝 Esc...amitlee9823
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...amitlee9823
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Pooja Nehwal
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...gajnagarg
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Researchmichael115558
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteedamy56318795
 
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 

Último (20)

Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men 🔝Bangalore🔝 Esc...
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men  🔝Bangalore🔝   Esc...➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men  🔝Bangalore🔝   Esc...
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men 🔝Bangalore🔝 Esc...
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
 

Learning R

  • 1. Learning R Kamal Gupta Roy Last Edited on Oct 17, 2021 Instructions / Agenda 1. Create Simple Arithmetic Operations. [+, -, *, /, ˆ, %%] 2. Create Logical Operation [<, >, < 1, > 1] 3. Built-in functions [sqrt, log, exp, floor, round, ceiling, abs] 4. Assignment of values to variables [a <- 2, b <- 3] 5. Retest arithmetic operations using variables 6. Saving arithmetic operations of two variables into a new variable 7. Introdution to Vector, vec <- c(23,4,12,78) 8. Finding values in the vectors using positions 9. Arithmetic Operations on vectors: addition, subtraction, multiplication 10. In built functions: mean(), median(), max(), min(), sort(), var(), sd(), cor, which.min and which.max 11. Numeric Vector, Character Vector, combining the two, class function 12. creating a vector using function seq [seq(10,70,by=10), seq(10,70,length=10)], help on seq func- tion 13. Directory functions [getwd(), setwd()] 14. Read a csv file; split columns into vectors and do some basic formulation 15. Exercises Simple Arithmetic Operations 5 + 2 ## [1] 7 5 * 2 ## [1] 10 1
  • 2. 5 - 2 ## [1] 3 5 / 2 ## [1] 2.5 5 %% 2 ## [1] 1 5 ˆ 2 ## [1] 25 Logical Operations 2<1 ## [1] FALSE 1<2 ## [1] TRUE (2<1)*1 ## [1] 0 (1<2)*1 ## [1] 1 Built in Functions sqrt(26) ## [1] 5.09902 log(2) ## [1] 0.6931472 2
  • 3. exp(2) ## [1] 7.389056 floor(2.467) ## [1] 2 round(2.467,2) ## [1] 2.47 ceiling(3.723) ## [1] 4 abs(-1) ## [1] 1 Assignment of Variables a <- 2 b <- 7 a + b ## [1] 9 a - b ## [1] -5 a ˆ b ## [1] 128 c <- a ˆ b c ## [1] 128 Vector Introduction 3
  • 4. vec <- c(23,4,12,78,4,98,12,34,52,73,59,30) length(vec) ## [1] 12 vec[7] ## [1] 12 vec[-3] ## [1] 23 4 78 4 98 12 34 52 73 59 30 vec[15] ## [1] NA vec[2:4] ## [1] 4 12 78 vec[c(5,8)] ## [1] 4 34 Arithmetic Operations on vectors quiz1 <- c(12,34,27,31) quiz2 <- c(34,16,22,11) quiz1 + quiz2 ## [1] 46 50 49 42 quiz1 - quiz2 ## [1] -22 18 5 20 quiz1/quiz2 ## [1] 0.3529412 2.1250000 1.2272727 2.8181818 4
  • 5. round(quiz1/quiz2,1) ## [1] 0.4 2.1 1.2 2.8 quiz1*quiz2 ## [1] 408 544 594 341 In built functions vec <- c(12,34,27,31,55,11,99,67,45,27) mean(vec) ## [1] 40.8 median(vec) ## [1] 32.5 max(vec) ## [1] 99 min(vec) ## [1] 11 sort(vec) ## [1] 11 12 27 27 31 34 45 55 67 99 sort(vec,decreasing = TRUE) ## [1] 99 67 55 45 34 31 27 27 12 11 cor(quiz1,quiz2) ## [1] -0.9333819 var(vec) ## [1] 725.9556 5
  • 6. sd(vec) ## [1] 26.94356 what is marks of a student in quiz1 who scored highest/least in quiz2? which.min(quiz2) ## [1] 4 i <- which.min(quiz2) quiz1[i] ## [1] 31 x <- cbind(quiz1,quiz2) x ## quiz1 quiz2 ## [1,] 12 34 ## [2,] 34 16 ## [3,] 27 22 ## [4,] 31 11 cor(x) ## quiz1 quiz2 ## quiz1 1.0000000 -0.9333819 ## quiz2 -0.9333819 1.0000000 Combining Vectors numb <- c(1,3,5,7) chara <- c("A","B","C") comb <- c(numb,chara) class(numb) ## [1] "numeric" class(chara) ## [1] "character" 6
  • 7. class(comb) ## [1] "character" Vectors creating from function seq s1 <- seq(10,70,by=10) s1 ## [1] 10 20 30 40 50 60 70 s2 <- seq(10,70,length=7) s2 ## [1] 10 20 30 40 50 60 70 rand <- c(floor(runif(n = 50, min = 1, max = 10))) rand ## [1] 8 4 1 2 8 2 9 5 4 2 8 2 7 4 4 3 9 6 8 5 4 7 6 1 5 4 9 6 2 7 3 4 9 7 5 3 4 7 ## [39] 1 2 3 3 3 9 9 2 1 9 9 5 #?seq #help(seq) Operations on vectors s1<-seq(1,20, length=5) s2<-seq(40,60, length=5) s1 + s2 ## [1] 41.00 50.75 60.50 70.25 80.00 s1*s2 ## [1] 40.00 258.75 525.00 838.75 1200.00 sum(s1*s2) ## [1] 2862.5 Naming of Vector 7
  • 8. # Create a card vector as a character vector and a number vector c("Jack","King","Queen","Ace") card <- c("Jack","King","Queen","Ace") cardn <- c(11,13,12,14) card ## [1] "Jack" "King" "Queen" "Ace" cardn ## [1] 11 13 12 14 #Giving a name to the cardn vector names(cardn) <- card #Option 2: cardn <- c("Jack" = 11,"King" = 13,"Queen" = 12,"Ace" = 14) #Option 3: cardn <- c(Jack = 11, King = 13, Queen= 12, Ace = 14) Directory Details #### workspace ls() ## [1] "a" "b" "c" "card" "cardn" "chara" "comb" "i" "numb" ## [10] "quiz1" "quiz2" "rand" "s1" "s2" "vec" "x" #To know what is the default working directory getwd() ## [1] "C:/Users/Debzitt/Desktop" # Setting a Working Directory using setwd() #setwd(C:/Users/Admin/) getwd() ## [1] "C:/Users/Debzitt/Desktop" Read a csv file df <- read.csv("veg_prices.csv") df 8
  • 9. ## ITEM QUANTITY UNIT_PRICE ## 1 Bangalore Tomato (Bangalore Thakkali) 3 17.44 ## 2 Beans (Beans) 5 20.14 ## 3 Beetroot (Beetroot) 5 14.05 ## 4 Bitter Gourd (Pavakkai) 5 18.69 ## 5 Bottle Gourd (Suraikai) 5 10.99 ## 6 Brinjal (Kathirikkai) 5 11.99 ## 7 Broad Beans (Avarakkai) 3 19.49 Calculate total bill from veg prices by splitting the dataframe into vectors sum(df$QUANTITY * df$UNIT_PRICE) ## [1] 490.09 Exercises Exercise 1 There are two sections. Assign random number to number of girls and number of boys in each section. Calculate: a. Total number of students in each section b. Find out which section has more girls, boys and students Exercise 2 Create a random numeric vector of atleast length of 10. Values should vary between 01-99. Calculate: a. what is the 5th value in the vector? b. What is the last value in the vector? c. What is the value of 3rd position from last? d. what is the difference between 8th and 2nd value of the vector? e. How many numbers are > 50? f. How many numbers are more than average of the vector? g. How many numbers are divisible by 3? 9
  • 10. Exercise 3 Create a vector with marks in the class (atleast 10 numbers, varying between 00-99) all values should be different. a. What is the maximum value? b. What is the minimum value? c. What is the range? d. What are the average marks? e. If maximum value is replaced by 82, what is the new average? Is your average increased or decreased? f. If another value 82 is added as a part of the vector, what is the new average? Is your average increased or decreased? Exercise 4 Calculate the following: a. Find the sum of first 5 numbers b. Find the sum of first 100 natural numbers. Use atleast two ways to find the sum c. Find the difference between sum of numbers between 51-100 and 1-50 Exercise 5 Prisha invests Rs 68,000 at 9% rate per annum for a certain period. Calculate: a. Total money Prisha gets after 5 years (Based on simple Interest) b. Total money Prisha gets after 5 years (Based on compound Interest, if compounded annually) Exercise 6 Aarav invests Rs 89,000 at 7% rate per annum for a two year lock-in period. Interest would be calculated compounded annually. Aarav needed money after 1 year and 10 months to pay for his college fees. He goes to the bank for withdrawl. Bank Manager told him that he would be given money back based on simple interest calculation. Also, Bank would charge a penalty of 10% of earned Interest. a. How much money Aarav would get after penalty? b. How much money he would have got more if he could wait for 2 more months? 10
  • 11. Exercise 7 Need to solve the quadratic equation. a. Give the roots of the equation, 2x2 + 7x + 3 = 0 #finding the maturity amount if 10% interest is compounded every year #n= time period new1.function = function(faceValue,rOfInterest,n) { marketValue <- faceValue*(1+(rOfInterest/100))^n print(marketValue) } new1.function(10000,10,10) #finding the maturity amount if 10% is calculated is as simple interest on face value new2.function = function(faceValue,rOfInterest,n) { marketValue <- faceValue + (faceValue*rOfInterest*n)/100 print(marketValue) } new2.function(10000,10,10) 11