Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

of

Learning R Slide 1 Learning R Slide 2 Learning R Slide 3 Learning R Slide 4 Learning R Slide 5 Learning R Slide 6 Learning R Slide 7 Learning R Slide 8 Learning R Slide 9 Learning R Slide 10 Learning R Slide 11
Upcoming SlideShare
What to Upload to SlideShare
Next

0 Likes

Share

Learning R

Learning R from scratch

  • Be the first to like this

Learning R

  1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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

Learning R from scratch

Views

Total views

197

On Slideshare

0

From embeds

0

Number of embeds

0

Actions

Downloads

0

Shares

0

Comments

0

Likes

0

×