R Functions
R Functions
# working dir
setwd("D:/R-BA")
# clear environment & history
rm(list = ls())
write("", file=".blank")
loadhistory(".blank")
unlink(".blank")
print
class
length
is.numeric
is.integer
is.character
is.vector
# vector of integers
c1 <- c(1L,2L,3L,4L,5L,6L,7L,8L,9L)
# vector of numeric / real numbers
c2 <- c(1,2,3,4,5,6,7,8,9)
# vector of numeric / real numbers
c3 <- c(1.1,2.2,3.3,4.4,5.5)
# vector of integers with :#
c4 <- c(1:20)
# vector of string
c5 <- c("aaa","bbb","ccc","ddd","xxx","yyy","zzz")
as.numeric
as.logical
as.character
c3
c3
c3
c3
<<<<-
c1
c1
c1
c1
+2
-2
*2
/2
is.factor
# Matrices
m <- matrix(1:6, nrow = 2, ncol = 3)
i <- 2L
m2 <- m1
m2 <- m1
m2 <- m1
m2 <- m1
+i
-i
*i
/i
dfr["1",]
dfr['2',]
dfr[1:2,]
dfr[-1,]
dfr[-3:-7,]
dfr[,1:2]
dfr[,-1]
dfr[,-2:-3]
dfr[,-3:-3]
numX <- 0 / 0
is.na(numX)
is.nan(numX)
# check inf
numX <- 1 / 0
is.na(numX) (This will be false if its infinite)
is.infinite(numX)
#cleaning NAs from single vector
vcbBad <- is.na(vciX) (This will give T, F Values)
vciX[!vcbBad] (This will convert back to Values)
vcbGood <- complete.cases(vciX, vcsY)
vciX[vcbGood] (It will compare vciX with all the completed case of vcbGood)
vcsY[vcbGood] (It will compare vcsY with all the completed case of vcbGood)
# Basic Functions with Data Frames
names(dfr)
head(dfr)
tail(dfr)
length(dfr)
length(dfr$foo)
nrow(dfr)
ncol(dfr)
attributes(dfr)
sum(dfr$bar)
min(dfr$bar)
max(dfr$bar)
median(dfr$bar)
mode(dfr$bar)
sd(dfr$bar)
summary(dfr)
data.frame(foo.sd=sd(dfr$foo),bar.sd=sd(dfr$bar),buf.sd=sd(dfr$buf))
# Ready To Use R Datasets
library(datasets)
data()
# use airquality dataset
airquality
head(airquality)
nrow(airquality)
nrow(dfrNifty)
ncol(dfrNifty)
# readLines
vcsUNProfile <- readLines("./data/un-profile.txt")
head(vcsUNProfile)
length(vcsUNProfile)
# view files
file.show("session-1.r")
file.show("./data/un-profile.txt")
file.show("./data/nifty-data.csv")
# readLines to read URL
conGoogle <- url("http://www.google.com/", "r")
vcsGoogle <- readLines(conGoogle)
close(conGoogle)
length(vcsGoogle)
head(vcsGoogle,10)
vcsGoogle[7]
#
#
#
#
#
#
#
#
#
Control Structures
Control Structures if, else (Testing a condition)
Control Structures for (execute a loop fixed number of times)
Control Structures while (execute a loop while a condition is true)
Control Structures repeat (execute an infinite loop)
Control Structures break (break the execution of the loop)
Control Structures skip (skip an iteration of a loop)
Control Structures return (exit a function)
User Defined Function
install.packages('dplyr')
install.packages('tidyr')
install.packages('data.table')
dfrNifty <- read.csv("G:/NMIMS/Sem 1/R/R-BA/data/nifty-data.csv", header=T,
stringsAsFactors=F)
dfrNifty <- data.table(dfrNifty)
# filter
dfrNifty.ACC <- filter(dfrNifty, Symbol == "ACC")
nrow(dfrNifty.ACC)
View(dfrNifty.ACC)
# subset of rows based on AND condition
dfrNifty.Filt <- filter(dfrNifty, (dfrNifty$DateDate >= "2014-12-01" & dfrNifty$DateDate <=
"2014-12-05") )
# subset of rows based on OR condition
dfrNifty.Filt <- filter(dfrNifty, (dfrNifty$DateDate == "2014-12-01" | dfrNifty$DateDate ==
"2014-12-05") )
# subset of rows based on "starts-with" condition ... start search string with ^
dfrNifty.Tmp <- slice(dfrNifty, 1:50)
dfrNifty.Filt <- filter(dfrNifty.Tmp, grepl("^TATA",dfrNifty.Tmp$NameOfTheSecurityInNse))
# subset of rows based on "ends-with" condition ... end search string with $
dfrNifty.Tmp <- slice(dfrNifty, 1:50)
dfrNifty.Filt <- filter(dfrNifty.Tmp, grepl("LTD$",dfrNifty.Tmp$NameOfTheSecurityInNse))
# subset of rows based on "contains" condition ... end search string with +
dfrNifty.Tmp <- slice(dfrNifty, 1:50)
dfrNifty.Filt <- filter(dfrNifty.Tmp, !grepl("BANK+",dfrNifty.Tmp$Symbol))
# subset of rows by position or row-range ... last 10
fr <- as.integer(nrow(dfrNifty)-9)
to <- nrow(dfrNifty)
dfrNifty.Slcd <- slice(dfrNifty, fr:to)