R Programming-1
R Programming-1
Objectives: To learn and Use R programming for statistical analysis of the data:
• Basic Computations in R
• Computing descriptive statistics of the data
• Summarize samples and tables
1. R Console: This area shows the output of code you run. Also, you can directly write
codes in console.
2. R Script: As the name suggest, here you get space to write codes. To run those codes,
simply select the line(s) of code and press Ctrl + Enter.
3. R environment: This space displays the set of external elements added. To check if data
has been loaded properly in R, always look at this area.
4. Graphical Output: This space display the graphs created during exploratory data
analysis.
Environment
R Script Editor History
Files, Plots
R Console
Packages
Help, Viewer
Key Points
# Use (Ctrl + L) to clear the R Console
# '#' is used for comments
# Use double backward slash "\\" or single forward slash "/" in commands
# Commands in R are case sensitive so its better to use either lower case always in file
and folder names
# R is insensitive to white spaces
Instructor: Dr. Prabhat Mittal M. Sc., M.Phil, Ph.D. (FMS, DU) 1|P a g e
Post-doctoral, University of Minnesota, USA
URL: http://people.du.ac.in/~pmittal/
# Loading of corresponding libraries is a pre-requisite to use the functions/arguments
provided in that particular library. These libraries are useful, although there are many.
Before starting your work you need to load the libraries first
# library(ggplot2)
# library(corrplot)
# library(psych)
# library(data.table)
# library(curl)
# library(agricolae)
# libraries can be installed both online (using the Tools-Install Packages-From
CRAN) and Offline using the downloaded zipped folders of the packages (using the
Tools-Install Packages-From .zip/.tar/.gz files).
Instructor: Dr. Prabhat Mittal M. Sc., M.Phil, Ph.D. (FMS, DU) 2|P a g e
Post-doctoral, University of Minnesota, USA
URL: http://people.du.ac.in/~pmittal/
Try the following R Script
setwd("D:\\Learning R")
getwd()
a = sqrt(729); a
b = 1947.0; b
b = as.character(b); b
vec = c(1:6); vec
c = c("Guru", "vignesh","durai","mittal","rishi","ramakar","aswini"); c
length(c)
c[c(1, 2)]
sort(c) #sorting method1
c[order(c)] #sorting method2
sort(c, decreasing = TRUE) #reverse sort
c[rev(order(c))] #reverse sort
d = c(4,6,5,7,10,9,4,15); d>7
p=c(3, 5, 6, 8)
q=c(3,3,3); p+q
age <- c(22, 25, 18, 20)
name <- c("Amit", "Rahul", "Neeti", "Komal")
gender <- c("M", "M", "F", "F")
details=data.frame(Age=age,Name=name,Gender=gender);details
z=0:9
digit=as.character(z)
as.integer(digit)
x=c(2,4,6,8)
y=c(TRUE,TRUE,FALSE,TRUE)
sum(x[y])
x1=c(4,6,5,7,10,9,4,15)
y1=c(0,10,1,8,2,3,4,1)
sum(x1*y1)
rbind(x1,y1)
a1=matrix(c(1:36),nrow = 6,ncol = 6); a1
a1[2,3]
a1[-2,-3]
a1[-2,3]
a1[,3]
a1[2,]
a1[2,-3]
a1[2,3]=14
a1[,3]=c(10,20,30,40,50,60)
a1[2,]=c(100,200,300,400,500,600); a1
Instructor: Dr. Prabhat Mittal M. Sc., M.Phil, Ph.D. (FMS, DU) 3|P a g e
Post-doctoral, University of Minnesota, USA
URL: http://people.du.ac.in/~pmittal/
fname="Prabhat"
lname="Mittal"
name=cat(fname,lname)
Names=c("Prabhat","Rahul","Amit")
designation=c("Professor","Project head","marketing head")
nd=data.frame(Names,designation);nd
init=c(2L,33,3+2i,TRUE,"String");
typeof(init);
init=c(2L,33,3+2i,TRUE);
typeof(init);
init=c(2L,33,TRUE);
typeof(init);
init=c(2L,TRUE);
typeof(init);
init=c(TRUE);
typeof(init);
#typeof -> order (character,complex,double,integer,logical)
#class -> order (character,complex,numeric,integer,logical)
int=c(2L,33,3+2i,TRUE,"String");
class(int);
int=c(2L,33,3+2i,TRUE);
class(int);
int=c(2L,33,TRUE);
class(int);
int=c(2L,TRUE);
class(int);
int=c(TRUE);
typeof(int);
x=c(20,30,15,NA,50,60,NA,70)
a=x[!is.na(x)]
na.omit(x)
b=seq(from = 0,to = 100,by = 2);b
c = seq(from = -10,to = 10,length.out = 20); c
d=seq(-10,20,by = 0.12);d
Instructor: Dr. Prabhat Mittal M. Sc., M.Phil, Ph.D. (FMS, DU) 4|P a g e
Post-doctoral, University of Minnesota, USA
URL: http://people.du.ac.in/~pmittal/
P4.R -Computing Descriptive Statistics
setwd("D:/………….") # Enter the complete path/link to working folder
getwd()
library(ggplot2)
library(corrplot)
library(psych)
library(data.table)
library(curl)
library(agricolae)
library(car)
library(ggpubr)
library(gridExtra)
library(readr)
datafile <- read_csv("D:/…….") # Enter the complete path/link to csv file (datafile)
datafile
Instructor: Dr. Prabhat Mittal M. Sc., M.Phil, Ph.D. (FMS, DU) 5|P a g e
Post-doctoral, University of Minnesota, USA
URL: http://people.du.ac.in/~pmittal/
attach(datafile)
mean(Weight)
sd(Weight)
#Grouping ggplots
p1=ggplot(datafile, aes(x = Group, y = Weight)) + geom_boxplot()
p2=ggplot(datafile, aes(x = Group, y = Height)) + geom_boxplot()
grid.arrange(p1,p2,ncol=2,nrow=1)
#Histograms
par(mfrow=c(1,3))
p4=hist(Weight,col="red")
p5=hist(Age, col="green")
p6=hist(Height,col="light green")
Instructor: Dr. Prabhat Mittal M. Sc., M.Phil, Ph.D. (FMS, DU) 6|P a g e
Post-doctoral, University of Minnesota, USA
URL: http://people.du.ac.in/~pmittal/