#Accessing the help files(Get Help) ?mean ?min ?rnorm ?colSums ?read.csv ?seq

#Search the help files for a word or phrase. help.search(‘weighted mean’) help.search(‘linear models’) help.search(“print”) help.search(“strsplit”)

#Find help for a package. help(package = ‘dplyr’) help(package=‘graphics’) help(package=‘spatial’) help(package= ‘dygraphs’)

#More about an object str(mtcars) class(mtcars) str(iris) class(iris) str(cars) class(cars) str(Nile) class(Nile)

vec=c(1,2,3) str(vec) class(vec)

#Using Packages install.packages(‘dplyr’) install.packages(‘reshape2’) install.packages(‘tidyquant’) install.packages(‘tidytext’)

dplyr::select reshape2::dcast tidyquant::tq_mutate graphics::barplot

#Working Directory

#getwd() #setwd(‘/Users/jeevanprakash/R progs’) #getwd()

#Vectors #Creating Vectors

A=c(52, 44, 11) A B=seq(2, 8, by=0.5) B C=rep(1:4, times=3) C D=rep(1:7, each=3) D

#Vectors Functions

A=c(4,6,3,734,1,34,31,3,21,78,94,31) sort(A) rev(A) table(A) unique(A)

#Selecting Vector Elements

A=c(4,6,3,734,1,34,31,3,21,78,94,31,‘apple’)

A[6] A[-3] A[6:10] A[-(1:3)] A[c(1, 5)]

A[A == 34] A[which(A==91)] A[A>30] A[A %in% c(1,3,21)] A[“apple”]

#For Loop

cars = list(“Benz”, “BMW”, “Audi”) for (x in cars) { if (cars == “BMW”) { break } print(x) }

#While Loop

y=5 while(y < 10) {
y = y + 1
print(y)
}

#If Statement

x=20
y=24 if(x>y) {
z=x; } else {
z=y; }
sprintf(“The greater no is: %d”,z)

#Functions

pow = function(x, y) { result = x^y print(paste(x,“raised to the power”, y, “is”, result)) }

pow(2,2)

#Reading and Writing Data

S = read.table(“http://www.sthda.com/upload/boxplot_format.txt”) S

write.table(S, ‘/Users/jeevanprakash/sample.txt’) read.table(“/Users/jeevanprakash/sample.txt”)

Sys.time()

#Reading csv File

df = read.csv(“/Users/jeevanprakash/Downloads/weather.csv”) df write.csv(df, ‘/Users/jeevanprakash/Downloads/oo.csv’)

#R DataFiles

save(study1.df, score.by.sex, study1.htest, file = ‘/Users/jeevanprakash/Downloads/study1.RData’)

load(‘/Users/jeevanprakash/Downloads/study1.RData’) study1.df study1.htest

#Conditions

x = 1 y = 2 if ( x==y ) { print (‘equal’) } else { print (‘not equal’) }

x = 1 y = 2 if ( x!=y ) { print (‘not equal’) } else { print (’ equal’) }

for (i in 1:3) { for (j in 1:i) { print(i * j) } }

#Jump Statement for (i in c(3, 6, 21, 0, 1, 45)) { if (i== 0) { break } print(i) }

#while loop

result = c(“Hello World”) i = 1

while (i < 6) {

print(result)

i = i + 1 }

#Greater than and less than

(1+2) > 4 “dog” < “Cats” TRUE <= FALSE (-6 * 5 + 2) >= (-10 + 1)

#Logic Operators

x=10 y=20 ! x x & y x && y x | y x || y xor(x, y) isTRUE (x) isFALSE(x)

is.na(x) is.null(x)

#Types

as.logical(2<3) as.numeric(“3”) as.integer(3.7) as.characher(2) as.factor(7)

#Maths Functions

log(x) exp(x) max(A) min(A) round(3.6343,2) signif(3.6343,3) cor(x, y) sum(A) mean(A) A=c(1,2,3,4,5) median(A) quantile(A) rank(A) var(A) sd(A) cos(x) sin(x) tan(x) ceiling(3.475) floor(3.475) trunc(5.99)

#Variable Assignment

a=‘apple’ a

#The Environment

ls() #rm(“seq1”) rm(list = ls()) ls()

#Lists

l = list(x = 10:50, y = c(‘a’, ‘b’)) l[[2]] l[1] l$y l[‘x’]

#Miscellaneous

16 = 3*5+1 18 %/%9 18 %%9

#Permutations x=c(1,2,3,4,5) y=c(6,7,8,9,10) sample(x, 4, replace =F) sample(c(10:50), 10, replace = TRUE) sort(x, decreasing = F) sort(c(50,44,34,12,1)) order(x, decreasing = F) order(c(5, 1, 7, 3))

#Function tests

replicate(10, exp(1000000)) system.time(exp(1000000))

#Strings

cat(x, y, sep = ’‘) cat(x, collapse =’’) grep(4, x) gsub(8,7, y) toupper(x) tolower(x) nchar(x)

#Factors

gender_vector = c(“Male”, “Female”, “Female”, “Male”, “Male”) factor(gender_vector)

cut(rep(1,5), 4)

#Statistics

df = data.frame( x= c(1,2,3,4,5),y= c(1,5,8,15,26)) # fit linear model lm(y ~ x^2, data=df)

counts = c(18,17,15,20,10,20,25,13,12) outcome = gl(3,1,9) treatment = gl(3,3) d.AD = data.frame(treatment, outcome, counts) glm(counts ~ outcome + treatment, family = poisson())

summary(linear_model)

t.test(1:10, y = c(7:20))

attach(airquality) Month <- factor(Month, labels = month.abb[5:9]) pairwise.t.test(Ozone, Month)

heads <- rbinom(1, size = 100, prob = .5) prop.test(heads, 100)

op <- options(contrasts = c(“contr.helmert”, “contr.poly”)) ( npk.aov <- aov(yield ~ block + NPK, npk) )

z<-seq(-3.5,3.5,0.1) # 71 points from -3.5 to 3.5 in 0.1 steps q<-seq(0.001,0.999,0.001) # 1999 points from 0.1% to 99.9% on 0.1% steps dStandardNormal <- data.frame(Z=z, Density=dnorm(z, mean=0, sd=1), Distribution=pnorm(z, mean=0, sd=1))
qStandardNormal <- data.frame(Q=q, Quantile=qnorm(q, mean=0, sd=1))
head(dStandardNormal)

lower<-qpois(0.001, lambda=2.5) upper<-qpois(0.999, lambda=2,5) n<-seq(lower,upper,1) q<-seq(0.001,0.999,0.001) dPoisson25 <- data.frame(N=n, Density=dpois(n, lambda=2.5), Distribution=ppois(n, lambda=2.5))
qPoisson25 <- data.frame(Q=q, Quantile=qpois(q, lambda=2.5))
head(dPoisson25)

lower<-qbinom(0.001, size=100, prob=0.5) upper<-qbinom(0.999, size=100, prob=0.5) n<-seq(lower,upper,1) q<-seq(0.001,0.999,0.001) dBinom100 <- data.frame(N=n, Density=dbinom(n, size=100, prob=0.5), Distribution=pbinom(n, size=100, prob=0.5))
qBinom100 <- data.frame(Q=q, Quantile=qbinom(q, size=100, prob=0.5))
head(dBinom100)

print(“Random 15 numbers between 1 and 3”) runif(15, min=1, max=3)

min <- 0 max <- 40 print (“Quantile Function Value”) qunif(0.2, min = min, max = max)

min <- 0 max <- 1 xpos <- seq(min, max , by = 0.02)
ypos <- qunif(xpos, min = 10, max = 100)
plot(ypos)

x <- 5:10 print (“dunif value”) dunif(x, min = 1, max = 20)

min <- 0 max <- 60 punif (15 , min =min , max = max, lower.tail=FALSE)

#Plotting

plot(7) plot(1:9) Temperature <- airquality$Temp hist(Temperature)

#Character Functions

x=“wjkbdejd” substr(x, 2, 4) grep(“A”, c(“A”,“k”,“c”), fixed=TRUE) sub(“\s”,“.”,“Hello There”) strsplit(“abc”, ““) paste(”Today is”, date()) toupper(x) tolower(x)

#Packages

library(ggplot2) ggplot(data = mtcars, aes(x = hp, y = mpg, col = disp))

library(stringr) x <- c(“why”, “video”, “cross”, “extra”, “deal”, “authority”) str_length(x)

library(readr) df = read_csv(“/Users/jeevanprakash/Downloads/weather.csv”)

Sys.time()