Tuesday, 7 February 2017

R - Basics









#Basic Calculations
#Removing if any ojects already exists in this current environment
#rm function is used to remove the ojects from the workspace
rm(list = ls())

#----------------------------
#Adding numbers
#----------------------------
1+3
#Adding numbers those are in Objects
Num1=10
Num2 = 15
Num3=Num1+Num2+5
Num3
#----------------------------
# Multiplications
#----------------------------
10*3
Num4= Num1*2
Num4
Num5= Num1*Num2
Num5
Num6= Num1*Num2*1.5
Num6
#----------------------------
#Reminder, As we known 10 devides 3 , remain is 1, syntax is a%%b
#----------------------------
remain<- 10%%3
remain
Rem1<- Num2%%Num1
#----------------------------
#division is used to know to division of Num1 divided by Num2, name is Modulos, syntax is a%/%b
#----------------------------
Div1 <- 10%/%3A
Div1
Div2 <- Num2%/%Num1
Div2
Div3 <- 100%/%30
Div3
Reminder3<- 100%%30
Reminder3
#----------------------------
#exponetials We read it as A to the Power of B , sytax is a^b ( shift+6 is the power symbol)
#----------------------------
10^2
10^3
a=10; b=3; c= 0.5;
Num_exp1<- (a^b)
Num_exp1
Num_exp2<- (a^b)^c
Num_exp2
#we can define how many decimals want to see in the output
Num_exp2
Num_exp2_3decimals <- round(Num_exp2, digits = 3)
Num_exp2_3decimals

#----------------------------
#Comparing 2 numbers, it yeilds result as boolean , it means TRUE or FALSE
#----------------------------
A1 = 100
A2=200
A3=300
A1>A2
A1<A2
#Comapring wherther 2 numners are equal ot not
A3== (A1+A2)





# Vector with 1 element, V1 vector holds data 1
V1<- 1
# Vector with more than 1 elements, V2_1 vector holds data 1,2,3,4,5,6
#using combination finction "c" (lower case c)
v2_1<- c(1,2,3,4,5,6)
#----- the below are
V2_2<- 1:6
V2_2
V2_3<- seq(from=1, to = 6, by=1)
V2_3
#Vector with string data
c1<- c('a','b', '', 'd','e',"F", 'g' )
#We can access wach element by using the index
#Accessing the 3rd element, i.e. no data for 3rd element
c1[3]
#assigning values, replacing 3rd element from null to c.
c1
c1[3]<-'c'
c1
#Replacing 6th element from F to f
c1[6]
c1[6]<- 'f'
c1[6]

# Similarly, we can do it for Number vector

#------------------------------------
#copying 1 object elements into another ojbect
O1 = c(1,2,3,4)
O2 = O1
O2





Monday, 6 February 2017

Date Calculations















==========================================



library(lubridate)
library(sqldf)
today()
CurrentYear_1stDate<-today()- yday(today()) +1
CurrentYear_1stDate
CurrentQuarter_1stDate<-today()- qday(today()) +1
CurrentQuarter_1stDate
CurrentMonth_1stDate<-today()- mday(today()) +1
CurrentMonth_1stDate
CurrentWeek_1stDate<-today()- wday(today()) +1
CurrentWeek_1stDate

# Importing Data from PC
Sample_Dataset<- read.csv(choose.files(), header = TRUE, sep = ",")
names(Sample_Dataset)
attach(Sample_Dataset)
YearToDate<- as.Date(ORD_DATE)>= CurrentYear_1stDate & as.Date(ORD_DATE)<= today()
QuarterToDate<- as.Date(ORD_DATE)>= CurrentQuarter_1stDate & as.Date(ORD_DATE)<= today()
MonthToDate<- as.Date(ORD_DATE)>= CurrentMonth_1stDate & as.Date(ORD_DATE)<= today()
WeekToDate<- as.Date(ORD_DATE)>= CurrentWeek_1stDate & as.Date(ORD_DATE)<= today()

FinalDataset<- cbind(Sample_Dataset,YearToDate,QuarterToDate,MonthToDate,WeekToDate)


#Renaming Newly Added Variable Names to avoid ambiguous while calling Vector or Column Names
names(FinalDataset)[length(FinalDataset)]
#Now Remaning the Variable Name "WeekToDate" to "WTD"
names(FinalDataset)[length(FinalDataset)]<- "WTD"
names(FinalDataset)[length(FinalDataset)]
names(FinalDataset)[length(FinalDataset)-1]
names(FinalDataset)[length(FinalDataset)-1]<- "MTD"
names(FinalDataset)[length(FinalDataset)-1]
#-----------
names(FinalDataset)[length(FinalDataset)-2]
names(FinalDataset)[length(FinalDataset)-2]<- "QTD"
names(FinalDataset)[length(FinalDataset)-2]
#-----------
#-----------

names(FinalDataset)[length(FinalDataset)-3]
names(FinalDataset)[length(FinalDataset)-3]<- "YTD"
names(FinalDataset)[length(FinalDataset)-3]
#Final dataset is having Flad whether the dataset is having YTD Data or MTD Data QTD data
#Now we are creating subsets to show YTD Exclusively
YTD_Sales<- subset(FinalDataset, subset= c(YTD== TRUE))
sqldf(" select ORD_DATE, Sales from YTD_Sales order by ORD_DATE ")

#-----
#------
#-------
#Now we are creating subsets to show QTD Exclusively
QTD_Sales<- subset(FinalDataset, subset= c(QTD== TRUE))
sqldf(" select ORD_DATE, Sales from QTD_Sales order by ORD_DATE ")
#-----
#------
#-------
#Now we are creating subsets to show MTD Exclusively
MTD_Sales<- subset(FinalDataset, subset= c(MTD== TRUE))
sqldf(" select ORD_DATE, Sales from MTD_Sales order by ORD_DATE ")




#names(Sample_Dataset)
#[1] "Order_ID"      "Ship_Mode"     "Customer_Name" "Segment"       "City"          "Product_Name"
#[7] "Sales"         "Quantity"      "Discount"      "Profit"        "ORD_DATE"      "SHIP_DATE"