Skip to content

Instantly share code, notes, and snippets.

@priyankajayaswal1
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save priyankajayaswal1/822922de78e33db5625d to your computer and use it in GitHub Desktop.
Save priyankajayaswal1/822922de78e33db5625d to your computer and use it in GitHub Desktop.
AreaIntegrator = function(A,kva,gorl=1)
{
if(gorl==1)
{
B = fn$sqldf("select * from A where Rep_KVA>$kva")
}
else if(gorl==-1)
{
B = fn$sqldf("select * from A where Rep_KVA<$kva")
}
if(length(B$Day)!=0)
{
B$Row_no = seq(1,length(B$Day))
B$ModifiedRep = ifelse((gorl*(B$Rep_KVA-kva)>0),(gorl*(B$Rep_KVA-kva)),0)
return(sum(B$ModifiedRep))
}
else
{
return(0.0)
}
}
CentralAnalyser <- function(z)
{
t=z
tn = as.numeric(sqldf("select count(distinct Day) from t"))
final=data.frame( Day=as.Date(character()),
Time=character(),
Rep_KVA=numeric(),
stringsAsFactors=FALSE)
for (j in 1:tn)
{
e = fn$sqldf("select * from t where Time=(select Time from t limit $j-1,1)")
for (i in 1:7)
{
f = fn$sqldf("select * from e where Date%7=$i-1 and Global_active_power!='NA'")
plot(f$Date,f$KVA,xlab='Day No.',ylab='KVA', main = "Graphical representation of KVA value in 6 months for particular time.")
lines(f$Date,f$KVA)
# Finding the best optimized central tendency for the dataset (for particular weekday and particular time)
nn = as.numeric(sqldf("select count(*) from f"))
hippo =d = NULL
for (i1 in seq(1,nn))
{
d=NULL
d1 = as.numeric(fn$sqldf("select KVA from f limit $i1-1,1"))
for (j1 in 1:nn)
{
d2 = as.numeric(fn$sqldf("select KVA from f limit $j1-1,1"))
d[j1]=ifelse(d2-d1>0,(d2-d1)*.16*.4,0)
}
f$hippo[i1]=sum(d)+d1*(nn)*0.1*.4
#k = k+1
#f$hippo[k]=hippo[i1]
#print(paste0("Hippo: for ", d1, " and ", d2," = ", hippo[i1]))
}
if(!is.na(f$hippo[1]))
{
rep_data=sqldf("select * from f where hippo=(select min(hippo)from f) limit 1")
repkva = as.numeric((sqldf("select KVA from rep_data")))
reptime = as.character(sqldf("select Time from rep_data"))
repweekday = weekdays(rep_data$Date)
final=rbind(final,data.frame(Day=repweekday,Time=reptime,Rep_KVA=repkva))
}
else
{
final=rbind(final,data.frame(Day=f$Day[1],Time=f$Time[1],Rep_KVA=0))
}
}
}
}
# Finding KVA min
PriceCalculation = function(tib,Hour,Set,Weekday="Sunday",Time="00:00 am")
{
library(Hmisc)
weekdaylist=c("Sunday", "Monday", "Tuesday","Wednesday","Thursday","Friday","Saturday")
# Daywise working
TotalPrice=0
jo=index=match(Weekday,weekdaylist)
time1=as.numeric(strsplit(Time,':')[[1]][1])
time2=(substring(strsplit(Time,':')[[1]][2],1,2))
if(length(grep('p', g, ignore.case = TRUE)))
{time1 = as.numeric(time1 +12)}
else { if(!(time1==10 || time1==11 || time1==12))
time1 = paste0("0",time1)}
cou=1
while( (jo>=index || (index+Set)%%7>jo) && cou<=7)
{
cou=cou+1
day1= weekdaylist[[jo]]
fuf=sqldf(paste0("select * from tib where Day='",day1,"'"))
fuf$Row_no=seq(1:length(fuf$Row_no))
if (is.na(Weekday))
{Weekday=day1}
PlanLabel=as.character(paste0(day1," - ",time1,":",time2,":00"))
divtocover=Hour*4
Point1=as.integer(fn$sqldf(paste0("select Row_no from fuf where Label='",PlanLabel,"'")))
# Block Table
Table1=fn$sqldf(paste0("select * from fuf where Row_no>=$Point1 and Row_no<($Point1+$divtocover)"))
# Index Table
Table2=fn$sqldf(paste0("select * from fuf where Row_no<$Point1 or Row_no>=($Point1+$divtocover)"))
BlockP=Hour*0.4*0.1*4
if(length(Table1$Row_no)!=0)
{
mkva=MinKvaCalculation(Table1)
p=mkva[[1]]
ond=0
while(p>=mkva[[1]] && p<=(mkva[[2]])+1 )
{
if(p*divtocover*.10<=AreaIntegrator(Table1,kva=p)*.16)
{
ond[p]=AreaIntegrator(Table1,kva=p,gorl=-1)
p=p+1
}
else{
ond[p]=0
break}
}
usekva=mkva[1]+(match(min(ond),ond))-1
IndexP1=AreaIntegrator(Table1,kva=usekva)*.16*.4
}
else
IndexP1=0
if(length(Table2$Row_no)!=0)
{
IndexP2=AreaIntegrator(Table2,kva=0)*.16*.4
}
else IndexP2=0
TotalPrice[jo]=BlockP+IndexP1+IndexP2
if(jo==7)
jo=1
else jo=jo+1
}
if(Set==5)
{
Table3=fn$sqldf(paste0("select * from tib where Day='",weekdaylist[jo],"'"))
TotalPrice[jo]=AreaIntegrator(Table3,kva=0)*.16*.4
Table4=fn$sqldf(paste0("select * from tib where Day='",weekdaylist[jo+1],"'"))
TotalPrice[jo+1]=AreaIntegrator(Table4,kva=0)*.16*.4
}
return(sum(TotalPrice))
}
MinKvaCalculation = function(tob)
{
nom=as.numeric(sqldf("select count(*) from tob"))
hr=nom/4
mink=as.numeric(sqldf("select Min(Rep_KVA) from tob"))
maxk=as.numeric(sqldf("select Max(Rep_KVA) from tob"))
return(c(floor(mink),ceiling(maxk)))
}
#Creating price sheet
pricesheet=NULL
pricesheet = rbind(pricesheet,data.frame(Hour=24,Size=7))
pricesheet = rbind(pricesheet,data.frame(Hour=24,Size=5))
pricesheet = rbind(pricesheet,data.frame(Hour=16,Size=7))
pricesheet = rbind(pricesheet,data.frame(Hour=16,Size=5))
pricesheet = rbind(pricesheet,data.frame(Hour=8,Size=7))
pricesheet = rbind(pricesheet,data.frame(Hour=8,Size=5))
pricesheet$Price=pricesheet$Hour*pricesheet$Size*.10*0.4
#UserChoice
choice = NULL
print(paste0("Enter number of blocks to be taken of your choice. "))
block= as.integer(readline())
for (g in 1:block)
{
print(paste0("Enter hour plan to be taken for block choice ",g))
choice$Hour[g] = as.numeric(readline())
print(paste0("Enter set plan to be taken for block choice ",g))
choice$Set[g] = as.numeric(readline())
if(choice$Hour[g]!=24)
{
print(paste0("Enter time plan to be taken for block choice ",g))
choice$Time[g] = as.character(readline())
}
else
{
choice$Time[g] = NA
}
if (choice$Set[g]==5)
{
print(paste0("Enter Start Day plan to be taken for block choice ",g))
choice$Day[g] = as.character(readline())
}
else
{
choice$Day[g] = NA
}
}
choice=data.frame(choice)
#Updating price column
choice$Price = choice$Hour*choice$Set*.10*0.4
#Updating Amount to be paid
for( oo in 1:length(choice$Hour))
choice$Amount[oo]=PriceCalculation(h3,Hour=choice$Hour[oo],Set=choice$Set[oo],Time=as.character(choice$Time[oo]))
library(sqldf)
# Reading content from file
x<-read.table("//home//innovator//Documents//Internship//Data//household_power_consumption.txt",header=TRUE,sep=';',dec = '.',stringsAsFactors=FALSE)
# Performing necessary conversions
x$Date<- as.Date(x$Date,"%d/%m/%Y")
x$Time = format(as.POSIXlt(x$Time, format = "%H:%M:%S"), format="%H:%M:%S")
x$Global_active_power = as.numeric(x$Global_active_power)
x$Global_reactive_power = as.numeric(x$Global_reactive_power)
# Defining KVA
x$KVA = sqrt(x$Global_active_power*x$Global_active_power+x$Global_reactive_power*x$Global_reactive_power)
# Creating Day column
x$Day = weekdays(x$Date)
## Creating sample pools for six month data
# The starting date
first<- as.Date('16-12-2008',format = '%d-%m-%Y')
# The Ending Date
second <- as.Date('30-06-2009',format = '%d-%m-%Y')
y = fn$sqldf("select * from x where Date>=$first and Date<=$second")
## Creating sample pools of data at time 0, 15min, 30min, 45min
z = fn$sqldf("select * from y where substr(Time,4,2)='00' or substr(Time,4,2)='15' or substr(Time,4,2)='30' or substr(Time,4,2)='45'")
Rearranger <- function(final)
{
# Rearranging the distinct Days in our sequence
weekdaylist= c( "Sunday","Monday","Tuesday", "Wednesday","Thursday","Friday","Saturday" )
yyy =sqldf("select distinct Day from final")
tutu=NULL
for (iii in 1:length(yyy$Day))
{
day = weekdaylist[iii]
if(grep(day, yyy$Day)!=0)
{
tut=data.frame(Day=day)
}
else
tut=NULL
tutu= rbind(tutu,tut)
}
yyy=tutu
# Created Dataset based on Daywise grouping
h3=NULL
for (h1 in 1:length(yyy$Day))
{
h0 = fn$sqldf("select * from final where Day=(select Day from yyy limit $h1-1,1)")
h2 = fn$sqldf("select * from h0 order by Time")
plot(h2$Time,h2$Rep_KVA,xlab="Time on Day",ylab="Rep_KVA",las=2)
lines(h2$Time,h2$Rep_KVA,xlab="Time on Day",ylab="Rep_KVA",las=2)
# Analysing this weekday for apt pricing plan
h3 = rbind(h3,h2)
}
# Labelling of the data
h3$Label=as.character(h3$Label)
h3=transform(Label=paste0(h3$Day," - ",h3$Time),h3)
# One week representation of Data
Row_no =seq(1,length(h3$Day))
h3 = cbind(Row_no,h3)
plot(h3$Row_no,h3$Rep_KVA)
lines(h3$Row_no,h3$Rep_KVA)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment