Last active
August 29, 2015 14:22
-
-
Save priyankajayaswal1/547af88a3883deac0d8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sqldf) | |
x<-read.table("//home//innovator//Documents//Internship//Data//household_power_consumption.txt",header=TRUE,sep=';',dec = '.',stringsAsFactors=FALSE) | |
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) | |
x$KVA = sqrt(x$Global_active_power*x$Global_active_power+x$Global_reactive_power*x$Global_reactive_power) | |
x$Day = weekdays(x$Date) | |
# Creating sample pools for six month data | |
first<- as.Date('16-12-2008',format = '%d-%m-%Y') | |
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'") | |
n = as.numeric(sqldf("select count(distinct Time) from z")) | |
rep_data = sqldf("select count(distinct Time) from z") | |
# Checking for which central tendency is apt | |
t = z | |
#t$Time = format(as.POSIXlt(t$Time, format = "%H:%M:%S"), format="%H:%M:%S") | |
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:n) | |
{ | |
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") | |
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 | |
#(forparticular weekday and particular time) | |
nn = as.numeric(sqldf("select count(*) from f")) | |
hippo =d = NULL | |
#k=0 | |
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]=(d2-d1)*.16 | |
} | |
f$hippo[i1]=sum(d) | |
#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 max(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)) | |
} | |
} | |
} | |
# Created Dataset based on Daywise grouping | |
h3=NULL | |
yyy =sqldf("select distinct Day from final") | |
for (h1 in 1:length(yyy$Day)) | |
{ | |
h2 = fn$sqldf("select * from finaldata where Day=(select Day from yyy limit $h1-1,1)") | |
h3 = rbind(h3,h2) | |
} | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sqldf) | |
x<-read.table("//home//innovator//Documents//Internship//Data//household_power_consumption.txt",header=TRUE,sep=';',dec = '.',stringsAsFactors=FALSE) | |
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) | |
x$KVA = sqrt(x$Global_active_power*x$Global_active_power+x$Global_reactive_power*x$Global_reactive_power) | |
x$Day = weekdays(x$Date) | |
# Creating sample pools for six month data | |
first<- as.Date('16-12-2008',format = '%d-%m-%Y') | |
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'") | |
n = as.numeric(sqldf("select count(distinct Time) from z")) | |
rep_data = sqldf("select count(distinct Time) from z") | |
# Checking for which central tendency is apt | |
t = z | |
#t$Time = format(as.POSIXlt(t$Time, format = "%H:%M:%S"), format="%H:%M:%S") | |
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:n) | |
{ | |
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") | |
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 | |
#(forparticular weekday and particular time) | |
nn = as.numeric(sqldf("select count(*) from f")) | |
hippo =d = NULL | |
#k=0 | |
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]=(d2-d1)*.16 | |
} | |
f$hippo[i1]=sum(d) | |
#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 max(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)) | |
} | |
} | |
} | |
# Created Dataset based on Daywise grouping | |
h3=NULL | |
yyy =sqldf("select distinct Day from final") | |
for (h1 in 1:length(yyy$Day)) | |
{ | |
h2 = fn$sqldf("select * from final where Day=(select Day from yyy limit $h1-1,1)") | |
h3 = rbind(h3,h2) | |
} | |
# One week representation of Data | |
h3=transform(Label=paste0(h3$Day," - ",h3$Time),h3) | |
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) | |
# Labelling Data properly | |
axis(1, at=h4$Row_no, labels=h4$Label,las=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sqldf) | |
x<-read.table("//home//innovator//Documents//Internship//Data//household_power_consumption.txt",header=TRUE,sep=';',dec = '.',stringsAsFactors=FALSE) | |
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) | |
x$KVA = sqrt(x$Global_active_power*x$Global_active_power+x$Global_reactive_power*x$Global_reactive_power) | |
x$Day = weekdays(x$Date) | |
# Creating sample pools for six month data | |
first<- as.Date('16-12-2008',format = '%d-%m-%Y') | |
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'") | |
n = as.numeric(sqldf("select count(distinct Time) from z")) | |
rep_data = sqldf("select count(distinct Time) from z") | |
# Checking for which central tendency is apt | |
t = z | |
#t$Time = format(as.POSIXlt(t$Time, format = "%H:%M:%S"), format="%H:%M:%S") | |
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:n) | |
{ | |
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") | |
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 | |
#(forparticular weekday and particular time) | |
nn = as.numeric(sqldf("select count(*) from f")) | |
hippo =d = NULL | |
#k=0 | |
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]=(d2-d1)*.16 | |
} | |
f$hippo[i1]=sum(d) | |
#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 max(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)) | |
} | |
} | |
} | |
# Created Dataset based on Daywise grouping | |
h3=NULL | |
yyy =sqldf("select distinct Day from final") | |
for (h1 in 1:length(yyy$Day)) | |
{ | |
h2 = fn$sqldf("select * from final where Day=(select Day from yyy limit $h1-1,1)") | |
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) | |
} | |
# One week representation of Data | |
h3=transform(Label=paste0(h3$Day," - ",h3$Time),h3) | |
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) | |
#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*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] = NULL | |
} | |
if (choice$Set[g]==5) | |
{ | |
print(paste0("Enter Start Day plan to be taken for block choice ",g)) | |
choice$Day[g] = as.character(readline()) | |
} | |
} | |
#Updating price column | |
choice$Price = choice$Hour*choice$Set*.10*4 | |
#Integrator for Area calculation | |
#A = dataframe for the dataset [example using final from above] | |
#kva = value above which data points are to be considered | |
AreaIntegrator = function(A,kva) | |
{ | |
B = fn$sqldf("select * from A where Rep_KVA>$kva") | |
B$Row_no = seq(1,length(B$Day)) | |
B$ModifiedRep = (B$Rep_KVA-kva)*.16 | |
return(sum(B$ModifiedRep)) | |
} | |
# Labelling Data properly | |
# axis(1, at=h4$Row_no, labels=h4$Label,las=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sqldf) | |
x<-read.table("//home//innovator//Documents//Internship//Data//household_power_consumption.txt",header=TRUE,sep=';',dec = '.',stringsAsFactors=FALSE) | |
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) | |
x$KVA = sqrt(x$Global_active_power*x$Global_active_power+x$Global_reactive_power*x$Global_reactive_power) | |
x$Day = weekdays(x$Date) | |
# Creating sample pools for six month data | |
first<- as.Date('16-12-2008',format = '%d-%m-%Y') | |
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'") | |
n = as.numeric(sqldf("select count(distinct Time) from z")) | |
rep_data = sqldf("select count(distinct Time) from z") | |
# Checking for which central tendency is apt | |
t = z | |
#t$Time = format(as.POSIXlt(t$Time, format = "%H:%M:%S"), format="%H:%M:%S") | |
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:n) | |
{ | |
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 | |
#(forparticular weekday and particular time) | |
nn = as.numeric(sqldf("select count(*) from f")) | |
hippo =d = NULL | |
#k=0 | |
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,0) | |
} | |
f$hippo[i1]=sum(d) + d1*(nn)*0.1 | |
#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 max(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)) | |
} | |
} | |
} | |
# Created Dataset based on Daywise grouping | |
h3=NULL | |
yyy =sqldf("select distinct Day from final") | |
for (h1 in 1:length(yyy$Day)) | |
{ | |
h2 = fn$sqldf("select * from final where Day=(select Day from yyy limit $h1-1,1)") | |
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) | |
} | |
# One week representation of Data | |
h3=transform(Label=paste0(h3$Day," - ",h3$Time),h3) | |
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) | |
#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*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] = NULL | |
} | |
if (choice$Set[g]==5) | |
{ | |
print(paste0("Enter Start Day plan to be taken for block choice ",g)) | |
choice$Day[g] = as.character(readline()) | |
} | |
} | |
#Updating price column | |
choice$Price = choice$Hour*choice$Set*.10*4 | |
#Integrator for Area calculation | |
#A = dataframe for the dataset [example using final from above] | |
#kva = value above which data points are to be considered | |
AreaIntegrator = function(A,kva) | |
{ | |
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((B$Rep_KVA-kva>0),(B$Rep_KVA-kva)*.16,0) | |
return(sum(B$ModifiedRep)) | |
} | |
else | |
{ | |
return(0.0) | |
} | |
} | |
# Value of Integrator for just Indexing System | |
Total_Price_on_indexing = AreaIntegrator(final,kva = as.numeric(sqldf("select min(Rep_KVA) from final")))*.16 | |
#Finding value of Rep_KVA such that area under that is equal to min block price plan area | |
optim = NULL | |
for (u in 1:length(final$Day)) | |
{ | |
final$optim[u]=Total_Price_on_indexing-AreaIntegrator(final,as.numeric(final$Rep_KVA[u])) | |
} | |
#Find ceiling and floor and possible KVA values height of block [which will be output] | |
# Labelling Data properly | |
# axis(1, at=h4$Row_no, labels=h4$Label,las=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sqldf) | |
x<-read.table("//home//innovator//Documents//Internship//Data//household_power_consumption.txt",header=TRUE,sep=';',dec = '.',stringsAsFactors=FALSE) | |
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) | |
x$KVA = sqrt(x$Global_active_power*x$Global_active_power+x$Global_reactive_power*x$Global_reactive_power) | |
x$Day = weekdays(x$Date) | |
# Creating sample pools for six month data | |
first<- as.Date('16-12-2008',format = '%d-%m-%Y') | |
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'") | |
n = as.numeric(sqldf("select count(distinct Time) from z")) | |
rep_data = sqldf("select count(distinct Time) from z") | |
# Checking for which central tendency is apt | |
t = z | |
#t$Time = format(as.POSIXlt(t$Time, format = "%H:%M:%S"), format="%H:%M:%S") | |
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:n) | |
{ | |
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 | |
#(forparticular weekday and particular time) | |
nn = as.numeric(sqldf("select count(*) from f")) | |
hippo =d = NULL | |
#k=0 | |
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)) | |
} | |
} | |
} | |
# Created Dataset based on Daywise grouping | |
h3=NULL | |
yyy =sqldf("select distinct Day from final") | |
for (h1 in 1:length(yyy$Day)) | |
{ | |
h2 = fn$sqldf("select * from final where Day=(select Day from yyy limit $h1-1,1)") | |
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) | |
} | |
# One week representation of Data | |
h3=transform(Label=paste0(h3$Day," - ",h3$Time),h3) | |
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) | |
#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()) | |
} | |
} | |
choice=data.frame(choice) | |
#Updating price column | |
choice$Price = choice$Hour*choice$Set*.10*0.4 | |
#Integrator for Area calculation | |
#A = dataframe for the dataset [example using final from above] | |
#kva = value above which data points are to be considered | |
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((B$Rep_KVA-kva>0),(gorl*(B$Rep_KVA-kva)),0) | |
return(sum(B$ModifiedRep)) | |
} | |
else | |
{ | |
return(0.0) | |
} | |
} | |
# Value of Integrator for just Indexing System | |
Total_Price_on_indexing = AreaIntegrator(final,kva = 0.0)*.16*0.4 | |
#Finding value of Rep_KVA such that area under that is equal to min block price plan area | |
no=length(final$Day) | |
for (u in 1:no) | |
{ | |
final$optim[u]=(AreaIntegrator(final,as.numeric(final$Rep_KVA[u]))*.16*.4+final$Rep_KVA[u]*no*.4*.1) | |
final$Unusedmoney[u]=final$optim[u]-Total_Price_on_indexing | |
} | |
#Find ceiling and floor and possible KVA values height of block [which will be output] | |
# Labelling Data properly | |
# axis(1, at=h4$Row_no, labels=h4$Label,las=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sqldf) | |
x<-read.table("//home//innovator//Documents//Internship//Data//household_power_consumption.txt",header=TRUE,sep=';',dec = '.',stringsAsFactors=FALSE) | |
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) | |
x$KVA = sqrt(x$Global_active_power*x$Global_active_power+x$Global_reactive_power*x$Global_reactive_power) | |
x$Day = weekdays(x$Date) | |
# Creating sample pools for six month data | |
first<- as.Date('16-12-2008',format = '%d-%m-%Y') | |
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'") | |
n = as.numeric(sqldf("select count(distinct Time) from z")) | |
rep_data = sqldf("select count(distinct Time) from z") | |
# Checking for which central tendency is apt | |
t = z | |
#t$Time = format(as.POSIXlt(t$Time, format = "%H:%M:%S"), format="%H:%M:%S") | |
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:n) | |
{ | |
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 | |
#(forparticular weekday and particular time) | |
nn = as.numeric(sqldf("select count(*) from f")) | |
hippo =d = NULL | |
#k=0 | |
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)) | |
} | |
} | |
} | |
# Created Dataset based on Daywise grouping | |
h3=NULL | |
yyy =sqldf("select distinct Day from final") | |
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) | |
} | |
# One week representation of Data | |
h3=transform(Label=paste0(h3$Day," - ",h3$Time),h3) | |
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) | |
#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 | |
#Integrator for Area calculation | |
#A = dataframe for the dataset [example using final from above] | |
#kva = value above which data points are to be considered | |
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((B$Rep_KVA-kva>0),(gorl*(B$Rep_KVA-kva)),0) | |
return(sum(B$ModifiedRep)) | |
} | |
else | |
{ | |
return(0.0) | |
} | |
} | |
# Value of Integrator for just Indexing System | |
Total_Price_on_indexing = AreaIntegrator(h3,kva = 0.0)*.16*0.4 | |
# Finding optimum for 24 * 7 plan | |
no=length(h3$Day) | |
for (u in 1:no) | |
{ | |
h3$optim[u]=(AreaIntegrator(h3,as.numeric(h3$Rep_KVA[u]))*.16*.4+h3$Rep_KVA[u]*no*.4*.1) | |
h3$Unusedmoney[u]=h3$Rep_KVA[u]*no*.4*.1-(AreaIntegrator(h3,as.numeric(h3$Rep_KVA[u]),-1)*.1*.4) | |
} | |
PriceCalculation = function(tib,kvaheight,Hour,Set,Weekday,Time) | |
{ | |
#Creating a equivalent kvaheight column to for applicable plan in that duration | |
if(Hour==24) | |
{ | |
PlanLabel = Just Weekdays dependent # Create label set from given to Set+given | |
if(Set == 7) | |
tib$KVAheight=ifelse(tib$Rep_KVA-kvaheight>0,tib$Rep_KVA-kvaheight,0) | |
} | |
else{ | |
if(Set==7) | |
PlanLabel = Just Time dependent # Create label set from given Time to Hour+given | |
} | |
PlanLabel = Label repetition based on Set amount | |
if(Set ==5) | |
{ | |
weekdaylist=c("Sunday", "Monday", "Tuesday","Wednesday","Thurday","Friday","Saturday") | |
for(ooo in 1:length(weekdaylist)) | |
{ | |
if(Weekday==weekdaylist[ooo]) | |
{ | |
index = (ooo) | |
} | |
} | |
labelweek = weekdaylist[index:index+Set] | |
Weekday=capitalize(tolower(Weekday[1][1])) | |
} | |
planlabel=paste0(Weekday," - ",Time) | |
BlockP=(kvaheight*0.4*Hour*Set*4*0.1) | |
IndexP=Are | |
} | |
# Generalising optimising procedure for any plan set | |
# Task FindLabel | |
time=as.numeric(strsplit(g,':')[[1]][1]) | |
if(length(grep('p', g, ignore.case = TRUE))) | |
{ | |
time = as.numeric(time +12) | |
}else { | |
time = paste0("0",time) | |
} | |
Weekday=capitalize(tolower(Weekday[1][1])) | |
PlanLabel=paste0(Weekday," - ",time,":00:00") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sqldf) | |
x<-read.table("//home//innovator//Documents//Internship//Data//household_power_consumption.txt",header=TRUE,sep=';',dec = '.',stringsAsFactors=FALSE) | |
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) | |
x$KVA = sqrt(x$Global_active_power*x$Global_active_power+x$Global_reactive_power*x$Global_reactive_power) | |
x$Day = weekdays(x$Date) | |
# Creating sample pools for six month data | |
first<- as.Date('16-12-2008',format = '%d-%m-%Y') | |
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'") | |
n = as.numeric(sqldf("select count(distinct Time) from z")) | |
rep_data = sqldf("select count(distinct Time) from z") | |
# Checking for which central tendency is apt | |
t = z | |
#t$Time = format(as.POSIXlt(t$Time, format = "%H:%M:%S"), format="%H:%M:%S") | |
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:n) | |
{ | |
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 | |
#(forparticular weekday and particular time) | |
nn = as.numeric(sqldf("select count(*) from f")) | |
hippo =d = NULL | |
#k=0 | |
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)) | |
} | |
} | |
} | |
# Rearranging the distinct Days in our sequence | |
weekdaylist= c( "Monday","Tuesday", "Wednesday","Thursday","Friday","Saturday","Sunday" ) | |
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) | |
} | |
# One week representation of Data | |
h3=transform(Label=paste0(h3$Day," - ",h3$Time),h3) | |
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) | |
#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 | |
#Integrator for Area calculation | |
#A = dataframe for the dataset [example using final from above] | |
#kva = value above which data points are to be considered | |
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((B$Rep_KVA-kva>0),(gorl*(B$Rep_KVA-kva)),0) | |
return(sum(B$ModifiedRep)) | |
} | |
else | |
{ | |
return(0.0) | |
} | |
} | |
# Value of Integrator for just Indexing System | |
Total_Price_on_indexing = AreaIntegrator(h3,kva = 0.0)*.16*0.4 | |
# Label creator | |
time=as.numeric(strsplit(g,':')[[1]][1]) | |
if(length(grep('p', g, ignore.case = TRUE))) | |
{ | |
time = as.numeric(time +12) | |
}else { | |
time = paste0("0",time) | |
} | |
Weekday=capitalize(tolower(Weekday[1][1])) | |
PlanLabel=paste0(Weekday," - ",time,":00:00") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Finding KVA min | |
PriceCalculation = function(tib,Hour,Set,Weekday,Time) | |
{ | |
library(Hmisc) | |
weekdaylist=c("Sunday", "Monday", "Tuesday","Wednesday","Thurday","Friday","Saturday") | |
# Daywise working | |
TotalPrice=0 | |
if(is.na(Weekday)) | |
{jo=weekdaylist[1]} else | |
{jo=index=match(Weekday,weekdaylist)} | |
while( jo>=index || (index+Set)%%7>jo) | |
{ | |
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} | |
if(is.na(Time)) | |
{Time='00:00 am'} else | |
{ | |
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)} | |
} | |
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(j==7) | |
j=1 | |
else jo=jo+1 | |
print(TotalPrice) | |
} | |
if(Set==5) | |
{ | |
Table3=fn$sqldf(paste0("select * from tib where Day=$(weekdaylist[jo])")) | |
TablePrice[jo]=AreaIntegrator(Table3,kva=0)*.16*.4 | |
Table4=fn$sqldf(paste0("select * from tib where Day=$(weekdaylist[jo+1])")) | |
TablePrice[jo+1]=AreaIntegrator(Table4,kva=0)*.16*.4 | |
} | |
print(TotalPrice) | |
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))) | |
} | |
for (iu in 0:) | |
{ | |
BlockP=Hour*Set*0.4*0.1*4 | |
# Making 3 tables and getting values from area integator for finding price. | |
tab1=fn$sqldf("select * from h3 where Row_no<$Point1") | |
tab2=fn$sqldf("select * from h3 where Row_no>$Point1 and Row_no<$Point2") | |
tab3=fn$sqldf("select * from h3 where Row_no>$Point2") | |
Index1=AreaIntegrator() | |
} | |
Optimize | |
# Not needed | |
for(ooo in 1:length(weekdaylist)) | |
{ | |
if(Weekday==weekdaylist[ooo]) | |
{ | |
index = (ooo) | |
} | |
} | |
weekdaylist= c( "Monday","Tuesday", "Wednesday","Thurday","Friday","Saturday","Sunday" ) | |
tutu=NULL | |
for (iii in 1:length(yyy$Day)) | |
{ | |
day = weekdaylist[iii] | |
if(length(grep(day, yyy$Day))) | |
{ | |
tut=data.frame(Day=day) | |
} | |
else | |
tut=NULL | |
tutu= rbind(tutu,tut) | |
} | |
yyy=tutu | |
Weekday=capitalize(tolower(Weekday[1][1])) | |
# Creating a equivalent kvaheight column to for applicable plan in that duration | |
if(Hour==24) | |
{ | |
PlanLabel = Just Weekdays dependent # Create label set from given to Set+given | |
if(Set == 7) | |
tib$KVAheight=ifelse(tib$Rep_KVA-kvaheight>0,tib$Rep_KVA-kvaheight,0) | |
} | |
else | |
{ | |
if(Set==7) | |
PlanLabel = Just Time dependent # Create label set from given Time to Hour+given | |
} | |
PlanLabel = Label repetition based on Set amount | |
if(Set ==5) | |
{ | |
labelweek = weekdaylist[index:(index+Set-1)] | |
} | |
BlockP=(kvaheight*0.4*Hour*Set*4*0.1) | |
IndexP=Are | |
labelweek=as.character(paste0(Weekday," - ",time1,":",time2,":00")) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sqldf) | |
x<-read.table("//home//innovator//Documents//Internship//Data//household_power_consumption.txt",header=TRUE,sep=';',dec = '.',stringsAsFactors=FALSE) | |
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) | |
x$KVA = sqrt(x$Global_active_power*x$Global_active_power+x$Global_reactive_power*x$Global_reactive_power) | |
x$Day = weekdays(x$Date) | |
# Creating sample pools for six month data | |
first<- as.Date('16-12-2008',format = '%d-%m-%Y') | |
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'") | |
n = as.numeric(sqldf("select count(distinct Time) from z")) | |
rep_data = sqldf("select count(distinct Time) from z") | |
# Checking for which central tendency is apt | |
t = z | |
#t$Time = format(as.POSIXlt(t$Time, format = "%H:%M:%S"), format="%H:%M:%S") | |
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:n) | |
{ | |
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") | |
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 | |
#(forparticular weekday and particular time) | |
nn = as.numeric(sqldf("select count(*) from f")) | |
hippo =d = NULL | |
#k=0 | |
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]=(d2-d1)*.16 | |
} | |
f$hippo[i1]=sum(d) | |
#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 max(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)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment