Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Basics of R
1. A Brief Introduction to R
Dr. Sachita Yadav
Associate Professor
Management Department
2. R, And the Rise of
the Best Software
Money Can’t Buy
R programming
language is a lot
like magic...
except instead of
spells you have
functions.
3. =
muggle
Like muggles, users of traditional stats software packages are
limited in their ability to change their environment. They have to
rely on algorithms that have been developed for them. The way
they approach a problem is constrained by how employed
programmers thought to approach them. And they have to pay
money to use these constraining algorithms.
4. =
wizard
R users are like wizards. They can rely on functions developed
for them by statistical researchers, but they can also create their
own. They don’t have to pay for the use of them, and once
experienced enough, they are almost unlimited in their ability to
change their environment.
5. R
Advantages Disadvantages
oNot user friendly at start - steep
learning curve.
oEasy to make mistakes
oWorking with large datasets is
difficult.
oFast and free
oAccess to R available whenever and
wherever need
oR packages
oR helps you think about data in ways
that are useful for statistical analysis
oR produces excellent, publication-
quality graphics
oR promotes reproducible (edited,
rerun, shared, commented) research.
oR is up-to-date
7. There are over 2000 add-on packages
This is an enormous advantage - new techniques available without
delay, and they can be performed using the R language.
Allows you to build a customized statistical program suited to your
own needs.
Downside = as the number of packages grows, it is becoming difficult
to choose the best package for your needs, & QC is an issue.
10. Getting Started
To Install Rstudio
Go to www.rstudio.com and click on the "Download RStudio"
button.
Click on "Download RStudio Desktop."
Click on the version recommended for your system, or the
latest Windows version, and save the executable file. Run the
.exe file and follow the installation instructions.
11. Installing Packages
Install the packages (Optional)
Run R studio.
Click on the Packages tab in the bottom-right
section and then click on install. The following
dialog box will appear.
In the Install Packages dialog, write the package
name you want to install under the Packages
field and then click install.
13. Operation Symbols
Symbol Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
%%
Modulo (estimates remainder in a
division)
^ Exponential
14. Objects in R
Objects in R obtain values by assignment.
This is achieved by the gets arrow, <-, and not the
equal sign, =.
Objects can be of different kinds.
15. Built in Functions
R has many built in functions that compute
different statistical procedures.
Functions in R are followed by ( ).
Inside the parenthesis we write the object (vector,
matrix, array, dataframe) to which we want to
apply the function.
16. Vectors
The very basic data types are the R-objects called vectors
Vectors are variables with one or more values of the same type.
When you want to create vector with more than one element, you should
use c() function which means to combine the elements into a vector.
# Create a vector.
apple <- c('red','green',"yellow")
print(apple)
# Get the class of the vector
print(class(apple))
When we execute the above code, it produces the following result −
[1] "red" "green" "yellow"
[1] "character"
17. Data Frame
Researchers work mostly with data frames
With previous knowledge you can built data frames in R
Also, import data frames into R.
Data frames are tabular data objects
Data Frames are created using the data.frame() function.
# Create the data frame.
BMI <- data.frame(gender = c("Male", "Male","Female"), height = c(152, 171.5, 165),
weight = c(81,93, 78),Age = c(42,38,26))
#print(BMI)
When we execute the above code, it produces the following result −
gender height weight Age
1 Male 152.0 81 42
2 Male 171.5 93 38
3 Female 165.0 78 26
18. Practice
A<-(“Welcome”) then press control R
x<-c(3,4,5,6)
Y<-c(3,4,5,6)
x+y run the command and get result
To check data type
a<-4 run data
Command is # Class(a) and run R to get result
19. Practical Example- Rainfall Zone
setwd("D:")
rain<-read.csv("rainfall_zonewise.CSV",header=TRUE)
fix(rain)
rain<-read.csv("rainfall_zonewise.CSV",header=TRUE)
dim(rain)
names(rain)
rain1<-rain[,c(1,2,4,384:386)]
fix(rain1)
dim(rain1)
#average rainfall for last 65 years
levels(rain1$DISTRICT)
rain.dist<-
aggregate(rain1$ANNUAL,by=list(rain1$DISTRICT),FUN=mean)
fix(rain.dist)
19
22. Learning R
Because R is interactive, errors are your friends!
MOST IMPORTANT - the more time we spend using R, the
more comfortable we become with it. After doing our first
real project in R, we won’t look back.