R: Introduction: Kedar Kelkar
R: Introduction: Kedar Kelkar
Kedar Kelkar
History of R
• R can be regarded as an implementation of the S language which was
developed at Bell Laboratories by Rick Becker, John Chambers and
Allan Wilks.
• This workspace is not saved on disk unless you tell R to do so. This means
that your objects are lost when you close R.
• If you save the workspace then all the objects in your current R session are
saved in a file .RData.
Installing R and R Studio
• Visit the following website:
• https://cran.r-project.org/bin/windows/base/
• Right-click the installer file and RUN as administrator.
• Follow the instructions for installation.
• Downloading RStudio
• https://www.rstudio.com/products/rstudio/download/#download
Interactive Mode in R
• Interactive mode is the most basic way of functioning in R.
• You type commands and immediately get results out of it.
• Using R as a calculator
• Multiplication: 2*3
• Division: 2/3
• Exponential: 2**3 or 2^3
• Remainder: 2%%3
1) 3+5+7*7-50/5
2) (3+5) + (7*7) - (50/5)
Computation in R
• Range
Note the
1) Assignment symbol
2) Case Sensitivity of R software.
Data Structures
• Vectors
• They are a Sequence of Objects
• One vector contains only one class of objects
Numerical Vector: v1 <- c(1, 2, 3, 4, 5)
String Vector: v2 <- c(“a”, “b”, “c”)
• Matrix
• One can define matrices of any number of rows and columns
matrix1 <- matrix(data=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), nrow = 4, ncol = 3)
Data Structures
• Lists
• R list is the object which contains elements of different types – like strings,
numbers, vectors and another list inside it.
• Data Frames
• A data frame is used for storing data tables. It is a list of vectors of equal
length.
Matrix and List
Data Frames
mpg Miles/(US) gallon
hp Gross horsepower
• Step 3: Multiply m1 by 3
• Addition:
• Add 3 to vec1
• Multiplication
• Multiply the vector by 5
• Median
• The positional center of the data set
• function median(x)
• Mode
• The observations which occurs with the highest frequency
• No in-built function for mode in R
Measures of Central Tendency
• Geometric Mean
• Unlike arithmetic mean, geometric mean uses products to find the mean.
• No in-built function
• prod(x)^(1/length(x))
• Harmonic Mean
• Reciprocal of the arithmetic mean of the reciprocals of x
• No in-built function.
Practice Question 4
• Discuss the Pseudo Code for the Harmonic mean.
• Prove: AM * HM = (GM)^2
Reading csv files in R
• data <- read.csv(file.choose())
• Homework
Find how to find median when frequencies and classes are given.
R Help
• ? For local search
• ?? For search in the entire documentation