[R] How to finding a given length of runs in a series of data?

2015-05-06 Thread jcrosbie
I'm trying to study times in which flow was operating at a given level or
greater. To do so I have created a way to see how long the series has
operated at a high level. But for some reason the data is calculating the
runs one hour to long. Any ideas on why? 





Code:
Date<-format(seq(as.POSIXct("2014-01-01 01:00"), as.POSIXct("2015-01-01
00:00"), by="hour"), "%Y-%m-%d %H:%M", usetz = FALSE)
Flow<-runif(8760, 0, 2300)

IsHigh<- function(x ){
if (x < 1600) return(0) 
if (1600 <= x) return(1) 
}

isHighFlow = unlist(lapply(Flow, IsHigh))

df = data.frame(Date, Flow, isHighFlow )


temp <- df %>%
  mutate(highFlowInterval = cumsum(isHighFlow==0)) %>%
  group_by(highFlowInterval) %>%
  summarise(hoursHighFlow = n(), minDate = min(as.character(Date)), maxDate
= max(as.character(Date))) 

#Then join the two tables together. 
temp2<-sqldf("SELECT * 
  FROM temp LEFT JOIN df 
  ON df.Date BETWEEN temp.minDate AND temp.maxDate")



--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-finding-a-given-length-of-runs-in-a-series-of-data-tp4706915.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Custom function causing an error with returning a ggplot object and Error in eval(expr, envir, enclos) : object '...' not found

2015-02-19 Thread jcrosbie
I'm trying to create a custom function to return a chart object. This
function seems to be having an error with calculating min/max/etc in the
ggplot object.

If I run the code for the ggplot not inside a custom function it works.

To reproduce this error after I need to clear the memory with (rm(list =
ls())) and reload the function and data.

How can I change my function to work correctly?

Sample data:

Date<-seq(as.Date("2000/1/1"), by = "week", length.out = 53*4)
ThousandBarrelsADay<-sample(1:1000, 53*4, replace=F)
yAxisTitle<-"Thousand Barrels per Day"
titleChart<-"test"
Function call:

p<-LinePlotTestStatsLine(Date, ThousandBarrelsADay, titleChart, yAxisTitle)
Error:

p
Error in eval(expr, envir, enclos) : object 'MinVal' not found

The code for the function:

LinePlotTestStatsLine<- function(xDateValues, yValues, titleChart,
yAxisTitle) {

dfTemp=0
#the sub title outlining the data range
subtitleChart = paste("(Data set from ", min(xDateValues), " to ",
max(xDateValues), ")", sep="")

#create a base dataframe
Week<- as.numeric(str_sub(ISOweek(xDateValues),start=-2))
dfTemp<-data.frame(xDateValues, Week, yValues)
dfTemp<- dfTemp[order(dfTemp$xDateValues),] 





#Summary Stat by week
dfTemp_Out<-describeBy(dfTemp$yValues, dfTemp$Week, mat = TRUE)
colnames(dfTemp_Out)[2]<-"Week"

#get the last year's of data Use 53 weeks because some years have 53 weeks
tempLast53<- tail(dfTemp, 53-length(dfTemp$yValues))
LableDateMinMax<-tempLast53$xDateValues[13]
LableDateMedian<-tempLast53$xDateValues[20]


#Chrate a base table for charting
ChartData1<-join(dfTemp_Out, tempLast53, type="inner")
#make sure the chart table is sorted
ChartData1<- ChartData1[order(ChartData1$xDateValues),] 


#find the max Date
MaxDate<- max(dfTemp$xDateValues)
maxYR<- max(year(dfTemp$xDateValues))

#min, Median, mean & max for hoizontal lines
MinVal<-min(dfTemp$yValues)
rMin<-max(which(dfTemp$yValues== MinVal, arr.ind = TRUE))
MinD<- dfTemp$xDateValues[rMin]

MaxVal<-max(dfTemp$yValues)
rMax<-max(which(dfTemp$yValues== MaxVal, arr.ind = TRUE))
MaxD<- dfTemp$xDateValues[rMax]

#Set the chart data
ChartData1_Plot <-ChartData1[,c("xDateValues","Week","yValues")]
ChartData1_Plot$Statistic<-paste("Past YR at ", MaxDate, sep="")


MedianVal<-median(dfTemp$yValues)
MeanVal<-mean(dfTemp$yValues)
stDev<- sd(dfTemp$yValues)
#ribbon to show one st. Dev. 
Ribbon<-data.frame(ChartData1[,c("xDateValues")])
Ribbon$Lower<-MeanVal-stDev
Ribbon$Higher<-MeanVal+stDev
colnames(Ribbon)<-c("xDateValues", "Lower", "Higher")
Ribbon$mean<-ChartData1$mean


#Set the seasons for charting
#Spring March 20, 2013
dSpring <- as.Date(paste("03/20/",maxYR, sep=""), "%m/%d/%Y")
if (MaxDate<=dSpring) { 
dSpring <- as.Date(paste("03/20/",maxYR-1, sep=""), "%m/%d/%Y")
}  

#summer June 21, 2013
dSummer<-as.Date(paste("06/21/",maxYR, sep=""), "%m/%d/%Y")
if (MaxDate<=dSummer) { 
dSummer<- as.Date(paste("06/21/",maxYR-1, sep=""), "%m/%d/%Y")
}  
#Autumn September 22, 2013 
dAutumn<-as.Date(paste("09/23/",maxYR, sep=""), "%m/%d/%Y")
if (MaxDate<=dAutumn) { 
dAutumn<- as.Date(paste("09/23/",maxYR-1, sep=""), "%m/%d/%Y")
}  

# winter December 21, 2013
dWinter<-as.Date(paste("12/21/",maxYR, sep=""), "%m/%d/%Y")
if (MaxDate<=dWinter) { 
dWinter<- as.Date(paste("12/21/",maxYR-1, sep=""), "%m/%d/%Y")
}  

ChartData_Plot <- ChartData1_Plot


p1<-ggplot(ChartData_Plot, aes(x=xDateValues,y=yValues))+
geom_line(aes(group=Statistic, colour=Statistic))+
scale_color_manual(values=c("black"))+
geom_ribbon(data=Ribbon, aes(group = 1, y=mean, x=xDateValues,
ymin=Lower, ymax=Higher), alpha=0.1, fill="blue")+
geom_hline(aes(yintercept=MinVal), color="red", linetype="dashed")+
geom_hline(aes(yintercept=MaxVal), color="red", linetype="dashed")+
annotate(geom="text", x = LableDateMinMax, y = MinVal-MaxVal/90, label =
paste("Min as at ", MinD, sep=""), colour = "red", size = 4)+
annotate(geom="text", x = LableDateMinMax, y = MaxVal+MaxVal/40, label =
paste("Max as at ", MaxD, sep=""), colour = "red", size = 4)+
geom_hline(aes(yintercept=MedianVal), color="darkgreen",
linetype="dashed")+
geom_hline(aes(yintercept=MeanVal), color="blue", linetype="dashed")+
annotate(geom="text", x = LableDateMinMax, y = MeanVal+MaxVal/40, label
= paste("Mean", sep=""), colour = "blue", size = 4)+
annotate(geom="text", x = LableDateMedian, y = MedianVal+MaxVal/40,
label = paste("Median", sep=""), colour = "darkgreen", size = 4)+
theme(legend.position="bottom")+
geom_vline(xintercept = as.numeric(dSpring),colour="darkgrey")+
geom_vline(xintercept = as.numeric(dSummer),colour="darkgrey")+
geom_vline(xintercept = as.numeric(dAutumn),colour="darkgrey")+
geom_vline(xintercept = as.numeric(dWinter),colour="darkgrey")+
annotate(geom="text", x = c(dWinter+45, dSpring+45, dSummer+45,
dAutumn+45), y = MaxVal+MaxVal/10, label = c("Winter",
"Spring", "Summer", "Autumn"), colour = "darkgrey",
size = 4)+
ggti

[R] R and GML data from the Canadian Goverment

2014-05-01 Thread jcrosbie
I'm trying to create a map of transmission lines in Alberta. In addition, I'm
very new to creating maps.

The data can be found at: http://geogratis.gc.ca/site/eng/download

http://ftp2.cits.rncan.gc.ca/pub/canvec/doc/CanVec_distribution_formats_en.pdf

Would someone be able to point me in the right direction? I haven't been
able to find an R package which is able to work with the GML data on the
webset.  Does anyone know of which package I should use and a good example
out there?

Thank you



--
View this message in context: 
http://r.789695.n4.nabble.com/R-and-GML-data-from-the-Canadian-Goverment-tp4689843.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How two have two legends on a chart with different groups?

2014-04-12 Thread jcrosbie
I'm trying to have two legends on a chart, with two sets of data with
subgroups. Currently ggplot is are merging the to sets of data together.

I would like to have the vertical lines in there own legend and not affect
the colour shading or show up in the "type" legend.

How would I go about doing this?

df <- data.frame(val1=rnorm(300, 75, 10), val2=rnorm(300, 75,
10),type=rep(c("A", "B", "C"),100))

cuts1 <- data.frame(Stats="Stat A", vals=c(43, 70, 90))
cuts2 <- data.frame(Stats="Stat B", vals=cuts2 <- c(46, 79, 86))

cuts <- rbind(cuts1,cuts2)

ggplot(df, aes(x=val1, y=val2, group=type)) +
geom_line(aes(colour=type))+
geom_point(aes(colour=type))+
geom_vline(data=cuts, 
 aes(xintercept=vals, 
 linetype=Stats,
 colour = Stats),
 show_guide = TRUE)
I got the idea of how to do this from: Add vline to existing plot and have
it appear in ggplot2 legend?



--
View this message in context: 
http://r.789695.n4.nabble.com/How-two-have-two-legends-on-a-chart-with-different-groups-tp4688666.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Subset error on atomic vectors why?

2014-04-02 Thread jcrosbie
I'm getting this error: "Error in MOPrice$Date : $ operator is invalid for
atomic vectors"

The cost is: subset(MOPrice,
as.Date(MOPrice$Date,"%Y-%m-%d")==as.Date("2013-11-28","%Y-%m-%d")) 

The date column looks like:
"2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"







--
View this message in context: 
http://r.789695.n4.nabble.com/Subset-error-on-atomic-vectors-why-tp4688051.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to get a subset with a date such as Date=11/03/2013 HE=02*?

2014-03-25 Thread jcrosbie
Sorry I thought I was being clear. 

These is how the time values in columns are downloaded from online
(http://ets.aeso.ca/). When there is an extra hour in the date there are two
hour ending twos in the that day (HE02 and HE02*). I did not create the data
this way.

What is a good way of dealing with data in this format?  

 
Col1<-c("11/02/2008", "11/02/2008", "11/02/2008", "11/02/2008")
Col2<-c("02*", "02", "03", "04") 
Col3SomeMadeupnumbers<-seq(1, 4, by=1)
df1 = data.frame(Col1, Col2,Col3SomeMadeupnumbers)


Col1<-c("11/02/2008 02*", "11/02/2008 02", "11/02/2008 03", "11/02/2008 04") 
df2 = data.frame(Col1, Col3SomeMadeupnumbers)


Also, Nov. 2, 2008 02 is when there was a time change. 





David Winsemius wrote
> On Mar 24, 2014, at 3:35 PM, jcrosbie wrote:
> 
>> I'm having a problem working with daylight savings dates in R.
>> 
>> I'm downloading data in two formats. 
>> 
>> Format One Col 1: a date such as "11/03/2013" col 2: Hour ending = 02*
>> Col1<-c("11/02/2008*", "11/02/2008", "11/02/2008", "11/02/2008")
>> Col1<-c("02*", "02", "03", "04")
>> 
>> Another data set is something like this: "11/03/2013 02*"
>> Col1<-c("11/02/2008 02*", "11/02/2008 02", "11/02/2008 03", "11/02/2008
>> 04")
>> 
>> These data frames are very big and over multiple years with multiple
>> values
>> for each hour ending. 
>> 
>> I'm trying to build subsets, filter, merge tables, lookup values with
>> dates
>> like these. How do I go about working with the "02*"?
>> 
> 
> If you are adding those asterisks to that example (as I suspect)  then you
> are just creating problems in understanding what is really in your data.
> Instead post output from dput(dat) for a section of an object named 'dat'.
> 
> 
>> View this message in context:
>> http://r.789695.n4.nabble.com/How-to-get-a-subset-with-a-date-such-as-Date-11-03-2013-HE-02-tp4687488.html
>> Sent from the R help mailing list archive at Nabble.com.
> 
> __

> R-help@

>  mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> -- 
> David Winsemius
> Alameda, CA, USA
> 
> __

> R-help@

>  mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.





--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-get-a-subset-with-a-date-such-as-Date-11-03-2013-HE-02-tp4687488p4687517.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to get a subset with a date such as Date=11/03/2013 HE=02*?

2014-03-24 Thread jcrosbie
I'm having a problem working with daylight savings dates in R.

I'm downloading data in two formats. 

Format One Col 1: a date such as "11/03/2013" col 2: Hour ending = 02*
Col1<-c("11/02/2008*", "11/02/2008", "11/02/2008", "11/02/2008")
Col1<-c("02*", "02", "03", "04")

Another data set is something like this: "11/03/2013 02*"
Col1<-c("11/02/2008 02*", "11/02/2008 02", "11/02/2008 03", "11/02/2008 04")

These data frames are very big and over multiple years with multiple values
for each hour ending. 

I'm trying to build subsets, filter, merge tables, lookup values with dates
like these. How do I go about working with the "02*"?



--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-get-a-subset-with-a-date-such-as-Date-11-03-2013-HE-02-tp4687488.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to importing a CVS file with multiple tables?

2014-03-20 Thread jcrosbie
I'm trying to figure out how to import data into a dataframe in R. 
The table I'm trying to import is:
HistoricalTradingReportServlet.csv

  

I would like to add two more column for each block of data to have the
HourEnding and date beside the data. 

How would I go about importing a table like this into R? 



line 1) is blank
line 2) is blank
line 3) Historical Bid data for: March 19, 2013.   In Hour Ending   01  

line 4) is blank
line 5 -infomratiom) Price after Last RestatementMW after Last 
Restatement  
Price Previous Day's Bid MW Previous Day's Bid
line 6) is blank
line 7) is blank
line 8-infomratiom) Historical Offer data for: March 19, 2013.   In Hour
Ending01
line 9) is blank
line 10-header) Price after Last Restatement MW after Last Restatement  
Price Previous Day's Offer   MW Previous Day's Offer

...Data.

[next table

line 1) is blank
line 2) Historical Bid data for: March 19, 2013.   In Hour Ending   01  

line 3) is blank
line 4 -infomratiom) Price after Last RestatementMW after Last 
Restatement  
Price Previous Day's Bid MW Previous Day's Bid
line 5) is blank
line 6) is blank
line 7-infomratiom) Historical Offer data for: March 19, 2013.   In Hour
Ending01
line 8) is blank
line 9-header) iPrice after Last Restatement MW after Last Restatement  
Price Previous Day's Offer   MW Previous Day's Offer
...Data.




--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-importing-a-CVS-file-with-multiple-tables-tp4687246.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to fix the warning message "the condition has length > 1 and only the first element will be used"?

2014-03-19 Thread jcrosbie
I'm trying to create a function to return the date x months in the past. 

With the code below I'm getting the warning message:

Warning message:
In if (MonthsBack >= CurrentMonth) { :
  the condition has length > 1 and only the first element will be used


##

DateBack <- function(CurrentYear, CurrentMonth, MonthsBack){
if(MonthsBack< 13){
if(MonthsBack>=CurrentMonth){
month<-12+CurrentMonth-MonthsBack
datet<-paste(CurrentYear-1,month, sep="-")
} 
else{
month<-CurrentMonth-MonthsBack
datet<-paste(CurrentYear-1, month, sep="-")
}
} 
else{
Years <- trunc(MonthsBack/12,0)
if((MonthsBack-12*Years)>=CurrentMonth){
month<-12+CurrentMonth-MonthsBack
} 
else{
month<-CurrentMonth-MonthsBack
}
datet<-paste(CurrentYear-Years, month, sep="-")
}
return(datet)
}

CurrentYear<- c(2000, 2000, 2003, 2004)
CurrentMonth<-c(1, 2, 6, 12) 

df<- data.frame(CurrentYear, CurrentMonth)

df$MonthsBack <- DateBack(df$CurrentYear,df$CurrentMonth,1)






--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-fix-the-warning-message-the-condition-has-length-1-and-only-the-first-element-will-be-used-tp4687140.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] why is as.date function not working for me? (dd/mm/yyyy h:mm)

2014-02-06 Thread jcrosbie
Thanks. as.POSIXct works for the most part. The only problem is part of data
I'm working has it's own time zone. Is there a way to not have a time zone
displayed? My times do not change with Daylight saving.



--
View this message in context: 
http://r.789695.n4.nabble.com/why-is-as-date-function-not-working-for-me-dd-mm--h-mm-tp4684874p4684890.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] why is as.date function not working for me? (dd/mm/yyyy h:mm)

2014-02-06 Thread jcrosbie
This function returns date/times without timezone
strptime(dates,format="%d/%m/%Y %H:%M")





--
View this message in context: 
http://r.789695.n4.nabble.com/why-is-as-date-function-not-working-for-me-dd-mm--h-mm-tp4684874p4684895.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] why is as.date function not working for me? (dd/mm/yyyy h:mm)

2014-02-06 Thread jcrosbie
Why am I know getting hours after I convert the date?

 dates <- c('31/12/2013 0:00',  '31/12/2013 1:00',  '31/12/2013 2:00', 
'31/12/2013 3:00',  '31/12/2013 4:00',  '31/12/2013 5:00',  '31/12/2013
6:00',  '31/12/2013 7:00',  '31/12/2013 8:00',  '31/12/2013 9:00',
'31/12/2013 10:00', '31/12/2013 11:00',  '31/12/2013 12:00', '31/12/2013
13:00', '31/12/2013 14:00', '31/12/2013 15:00',  '31/12/2013 16:00',
'31/12/2013 17:00', '31/12/2013 18:00', '31/12/2013 19:00', '31/12/2013
20:00', '31/12/2013 21:00', '31/12/2013 22:00', '31/12/2013 23:00', 
'01/01/2014 0:00')

as.Date(dates, format="%d/%m/%Y %H:%M")
 [1] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
 [6] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
[11] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
[16] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
[21] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2014-01-01"





--
View this message in context: 
http://r.789695.n4.nabble.com/why-is-as-date-function-not-working-for-me-dd-mm--h-mm-tp4684874.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Download data

2013-09-18 Thread jcrosbie
Thank you for all your help. I'm still not able to figure out how automate
downloads from online websites.

This is a daily function to download the needed data. I would also like to
be able to do this on other websites such as: 

http://ets.aeso.ca/ets_web/docroot/Market/Reports/HistoricalReportsStart.html

and

http://www.ngx.com/?page_id=561




--
View this message in context: 
http://r.789695.n4.nabble.com/Download-data-tp4668138p4676465.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Downloading CSV file - RCurl help

2013-06-15 Thread jcrosbie
There are some CSV file from the link below. 

I'm having trouble installing the package. Is this the package I have to use
or is there another one I need to use? If so how do I get this one loaded. 



https://www.enmax.com/Power/Energy+Retailers/Settlement+Reports/Profile+settlement+report.htm

 install.packages("RCurl", repos =
"http://cran.r-project.org/bin/windows/contrib/r-release/RCurl_1.95-4.1.zip";,
type="source")
Installing package into ‘C:/Users/James/Documents/R/win-library/3.0’
(as ‘lib’ is unspecified)
Warning: unable to access index for repository
http://cran.r-project.org/bin/windows/contrib/r-release/RCurl_1.95-4.1.zip/src/contrib
Warning message:
package ‘RCurl’ is not available (for R version 3.0.1) 



--
View this message in context: 
http://r.789695.n4.nabble.com/Downloading-CSV-file-RCurl-help-tp4669616.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Download data

2013-05-28 Thread jcrosbie
Hi, I'm trying to download data from:
http://www.ngx.com/settlehistory.html

Is it possible to fetch the data with R? 

Thank you



--
View this message in context: 
http://r.789695.n4.nabble.com/Download-data-tp4668138.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] ggplot add a legend?

2012-12-17 Thread jcrosbie

How to I go about adding a legend  to the following plot?

ggplot() +  geom_line(aes(x=df2010$Price,y=df2010$percentCapacity), colour =
"red") 
+ geom_line(aes(x=df2011$Price,y=df2011$percentCapacity), colour = "green")
+geom_line(aes(x=df2012$Price,y=df2012$percentCapacity), colour = "blue") 
+scale_x_log10()


 



--
View this message in context: 
http://r.789695.n4.nabble.com/ggplot-add-a-legend-tp4653389.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] custom function & missing value where TRUE/FALSE needed

2012-11-05 Thread jcrosbie
I can't figure out why this function wont work. 

#Custom Function used
fallInBand <- function(x){
  #Assume the bands fall go up form 0 to 100 in intervals of 5 then from 100
to 1000 in intervals of 100.
  #returns the location (band number)
  if (is.na(x) == FALSE) {
if(x < 100) {#bands of 5 
  if((x %% 5) >=0){#falls within the band
Result = as.integer(x/5)+1
  } else  {#falls on the band
Result = as.integer(x/5)
  }#end location within band if
  
  
} else { # bands of 100
  if((x %% 100) >0){#falls within the band
Result = as.integer(x/100)+21
  } else {#falls on the band
Result = as.integer(x/100)+20
  }#end location within band if
}#end  
  } else {
Result = NA
  }
  
  return(Result)
}#end function fallInBand

#Data example:
t = c(NA, 0, 10.44, 250, 30, 11.66, 20.18, 815, 629, 11.94, 10.4, 
10.41, 20.11, 11.9, 10.45, 11.01, 141, 444, 389, 148.02, 98.88, 
84.01, 388.02, 342, 339.06, 331, 188, 11.98, 10.95, 915, 14.65, 
99.99, 67, 865, 333, 276, 11.93, 765, 166, 259, 246, 321, 19.98, 
93, 186, 11.39, 928, 945, 869, 955, 612.11, 588, 944, 943, 678, 
836, 804.1, 11.15, 848, 696.41, 10.37, 10.98, 10.9, 298.18, 180.97, 
145.1, 895, 894, 30.14, 29.87, 21.12, 31.15, 31, 885, 115, 863, 
884, 828, 189, 20.07, 194, 411.15, 887, 879, 411.75, 898, 823, 
81.55, 875, 843, 131.68, 381, 281, 395, 105, 776, 240, 230.5, 
80.25, 212.18, 198.3, 74.25, 374, 803, 45.22, 36.44, 10.5, 21.15, 
10.78, 10.77, 38.17, 224, 72.4, 68, 399, 396.71, 42, 10.86, 11.99, 
12.1, 11.88, 9.72, 10.72, 9.7, 8.47, 7.78, 7.4, 7.01, 7.02, 7.53, 
10.85, 64.36, 888, 791, 735, 476, 10.75, 44, 895.1, 902, 584, 
8.75, 838, 752, 747, 691, 688, 684.15, 679, 14, 265, 214, 148, 
144.1, 136, 15.68, 348, 343, 112, 98, 855.35, 881, 891.6, 8.45, 
900, 8.22, 897.15, 125.11, 8.8, 477, 648, 638, 719, 698, 480, 
728.23, 779.31, 899.99, 9.35, 9.15, 9.3, 6.98, 904.35, 13.15, 
13.1, 868.23, 810, 796, 746, 744, 13, 10.81, 10.62, 10.25, 10.21, 
6.85, 6.8, 972.13, 11.13, 10.1, 10.15, 74.37, 954.33, 899.76, 
299.55, 914.41)

fallInBand(t)



--
View this message in context: 
http://r.789695.n4.nabble.com/custom-function-missing-value-where-TRUE-FALSE-needed-tp4648500.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] aggregate function not working?

2012-10-17 Thread jcrosbie
The aggregate function for some reason will now work for me. 

The error I'm getting is:
"Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?"



agPriceList=aggregate(PriceList$Size, list(PriceList$bandNum),sum)

*Price list dataframe:*
dput(PriceList)
structure(list(Price = c(0, 8.18, 8.27, 10.42, 10.5, 10.6, 11.13, 
11.18, 11.21, 11.35, 11.36, 11.67, 11.89, 11.97, 12.05, 12.11, 
12.14, 12.53, 12.63, 12.7, 12.82, 12.96, 13, 13.08, 13.12, 13.14, 
13.15, 13.2, 13.43, 13.78, 13.95, 14.04, 14.06, 14.17, 14.21, 
14.3, 14.38, 14.44, 14.85, 14.9, 14.95, 14.96, 14.99, 15.03, 
15.07, 15.19, 15.3, 15.62, 15.77, 15.79, 16.05, 16.18, 16.22, 
16.24, 16.81, 16.85, 16.89, 17.03, 17.04, 17.05, 17.07, 17.11, 
17.15, 17.21, 17.3, 17.37, 17.59, 17.85, 18, 18.05, 18.09, 18.2, 
18.25, 18.45, 18.79, 18.83, 18.9, 18.94, 18.95, 18.99, 19.05, 
19.08, 19.12, 19.15, 19.17, 19.27, 19.45, 19.8, 19.85, 20.11, 
20.22, 20.36, 20.59, 20.64, 20.74, 20.82, 20.96, 21.05, 21.12, 
21.18, 21.33, 21.35, 21.5, 21.77, 22.04, 22.3, 22.34, 22.42, 
22.55, 22.94, 23, 23.04, 23.14, 23.2, 23.29, 23.36, 23.42, 23.43, 
23.44, 23.45, 23.54, 23.55, 23.61, 23.77, 23.83, 23.9, 23.96, 
24, 24.01, 24.14, 24.5, 24.61, 24.76, 24.85, 24.99, 25, 25.6, 
25.61, 25.7, 26.09, 26.5, 26.72, 26.89, 29.5, 30.54, 40.63, 40.73, 
41.74, 43.5, 44.33, 45.33, 46.05, 47.11, 47.34, 49.58, 50.71, 
77, 86.68, 97, 133.6, 203.48), Size = c(664640L, 440L, 407L, 
180L, 690L, 851L, 190L, 480L, 720L, 74L, 111L, 1560L, 11400L, 
160L, 9333L, 55665L, 222L, 23662L, 210L, 12735L, 1757L, 63736L, 
18318L, 460L, 120L, 680L, 400L, 960L, 432L, 259L, 840L, 8200L, 
630L, 180L, 189L, 1490L, 27L, 8080L, 6291L, 240L, 841L, 5469L, 
4035L, 12555L, 5320L, 9249L, 240L, 1149L, 40L, 660L, 333L, 4360L, 
180L, 360L, 240L, 2900L, 120L, 2639L, 1221L, 10019L, 2990L, 259L, 
25L, 320L, 1684L, 305L, 40L, 27L, 4213L, 108L, 222L, 216L, 80L, 
148L, 135L, 970L, 222L, 240L, 1332L, 43875L, 210L, 162L, 185L, 
180L, 459L, 35854L, 3270L, 162L, 259L, 120L, 9130L, 2120L, 111L, 
222L, 595L, 888L, 222L, 189L, 210L, 1740L, 630L, 520L, 120L, 
9673L, 160L, 648L, 280L, 378L, 240L, 40L, 444L, 407L, 210L, 185L, 
180L, 148L, 110L, 486L, 148L, 756L, 100L, 2750L, 40L, 720L, 1440L, 
185L, 259L, 240L, 259L, 150L, 37L, 481L, 222L, 90L, 111L, 5598L, 
222L, 222L, 222L, 222L, 322L, 112L, 222L, 690L, 180L, 222L, 12L, 
18L, 22L, 12L, 22L, 6L, 30L, 62L, 20L, 30L, 12L, 185L, 10L, 185L, 
10L), bandNum = list(0L, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5L, 6, 6, 6, 6, 6, 6, 6, 6, 7, 9, 
9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 16, 18, 20, 21, 22)), .Names =
c("Price", 
"Size", "bandNum"), row.names = c(NA, -161L), class = "data.frame")



--
View this message in context: 
http://r.789695.n4.nabble.com/aggregate-function-not-working-tp4646527.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] subtotals based on price bands?

2012-10-17 Thread jcrosbie
Thank you, but I believe that's not returning what I want. 

This maybe a better dat aset to work with. 

seq1 = seq(0, 100, by = 5) 
seq2 = seq(100, 1000, by = 100) 
Bands = c(seq1, seq2) 


DF1 <- data.frame(matrix(ncol = 2, 200))
colnames(DF1)<- c("Price", "Size")
DF1$Price <_ sample(1:1000, 200, replace=F) 
DF1$Size <_ sample(1:1000, 200, replace=F)

I'm looking to find the subtotal of size when their price falls within a band. 

The bands go 0 to 5, 5 to 10,100 to 200,..., 900 to 1000. 

Therefore if we had:
Price Size
210
220
1120
1120

The total in this case would be
0 to 5 30
10 to 2040

Thank you








 From: arun kirshna [via R] 
To: jcrosbie  
Sent: Wednesday, October 17, 2012 10:32 AM
Subject: Re: subtotals based on price bands?


Hi, 
May be this helps: 
seq1 = seq(0, 100, by = 5) 
seq2 = seq(100, 1000, by = 100) 
Bands = c(seq1, seq2) 
set.seed(1) 
Prices = sample(1:1000, 200, replace=F) 
set.seed(1) 
size = sample(1:1000, 200, replace=F) 
Prices1<-cut(Prices,breaks=unique(Bands)) 
 tapply(size,Prices1,sum) 
#      (0,5]      (5,10]     (10,15]     (15,20]     (20,25]     
(25,30] 
  #       NA          NA          26          NA         
 23          NA 
    #(30,35]     (35,40]     (40,45]     (45,50]     (50,55]     
(55,60] 
      #   31          NA          NA          NA         
 54          NA 
    #(60,65]     (65,70]     (70,75]     (75,80]     (80,85]     
(85,90] 
      #  126          67          NA          79         
 82         179 
    #(90,95]    (95,100]   (100,200]   (200,300]   (300,400]   
(400,500] 
      #  186          NA        2627        5342       
 8927        9961 
  #(500,600]   (600,700]   (700,800]   (800,900] (900,1e+03] 
    #  10932       14913       18677       12833       16022 
table(Prices1) 
#Prices1 
   #   (0,5]      (5,10]     (10,15]     (15,20]     (20,25]     
(25,30] 
     #     0           0           2           0       
    1           0 
    #(30,35]     (35,40]     (40,45]     (45,50]     (50,55]     
(55,60] 
      #    1           0           0           0       
    1           0 
    #(60,65]     (65,70]     (70,75]     (75,80]     (80,85]     
(85,90] 
      #    2           1           0           1       
    1           2 
    #(90,95]    (95,100]   (100,200]   (200,300]   (300,400]   
(400,500] 
      #    2           0          17          22       
   25          22 
  #(500,600]   (600,700]   (700,800]   (800,900] (900,1e+03] 
    #     20          23          25          15         
 17 
A.K. 




If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/subtotals-based-on-price-bands-tp4646473p4646484.html
 
This email was sent by arun kirshna (via Nabble)
To receive all replies by email, subscribe to this discussion



--
View this message in context: 
http://r.789695.n4.nabble.com/subtotals-based-on-price-bands-tp4646473p4646505.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] subtotals based on price bands?

2012-10-17 Thread jcrosbie
I would like to create a subtotal table with custom bands. 

seq1 = seq(0, 100, by = 5) 
seq2 = seq(100, 1000, by = 100) 
Bands = c(seq1, seq2) 
#Prices 
Prices = sample(1:1000, 200, replace=F) 
#corresponding size for the given price above. 
size = sample(1:1000, 200, replace=F) 

How would  I find the subtotal of the size based on a given price falls
within a band? 



--
View this message in context: 
http://r.789695.n4.nabble.com/subtotals-based-on-price-bands-tp4646473.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] frequency table with custom bands

2012-10-16 Thread jcrosbie
I would like to create a frequency table with custom bands. 

seq1 = seq(0, 100, by = 5)
seq2 = seq(100, 1000, by = 100)
Bands = c(seq1, seq2)
Prices = sample(1:1000, 200, replace=F)

How would  I go about find the frequency of prices within each band? 





--
View this message in context: 
http://r.789695.n4.nabble.com/frequency-table-with-custom-bands-tp4646413.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] conditional sum two dataframes

2012-07-26 Thread jcrosbie
I have two data frames. One with a matrix of months and the other with a
matrix of values. The two dataframes correspond to each other. I would like
to sum up all the values in by month. 

What would be an efficient way to do this? 

a=C(2,3,5,2,3,5)
b=c(2,6,3,2,6,3)
c=c(2,6,7,2,6,5)

months <- data.frame(a,b,c)

a=C(200,345,5435,27456,3343,555)
b=c(288,699,377,234,6354,33454)
c=c(2345,6345,79678,245,676,5089)

values <- data.frame(a,b,c)




--
View this message in context: 
http://r.789695.n4.nabble.com/conditional-sum-two-dataframes-tp4637986.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Table/Frame - output

2012-07-17 Thread jcrosbie
I would like to output a nicely formatted data frame to a bitmap. 

Is this possible in R?  

--
View this message in context: 
http://r.789695.n4.nabble.com/Table-Frame-output-tp4636790.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] remove loop which compares row i to row i-1

2012-07-16 Thread jcrosbie
Thank you, That was very helpful. 

I do have another problem along the same lines. But I can not think of a way
to do this with  a function like ddply or aggregate.
 
Example:
x = sample(0:1,42,TRUE)
[1] 1 1 1 1 0 1 0 0 1 1 1 1 1 0 0 0 1 0 0 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1
1 1 0 0 0 0
I want to find create a new vector such that the sums of the 1's stops each
time there is a 0 and starts again next time there is a one. 
Output would be:
4,1, 5, 1,2,2,1,2,3



--
View this message in context: 
http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327p4636662.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] remove loop which compares row i to row i-1

2012-07-12 Thread jcrosbie
I'm sorry but I see to be getting an error. 

> data.tmp <- aggregate(MyTo ~ Date + Hour, data = tUnitsort[, cols], max) 
> Error in `[.default`(xj, i) : invalid subscript type 'builtin'





 From: Rui Barradas [via R] 
To: jcrosbie  
Sent: Thursday, July 12, 2012 3:12 PM
Subject: Re: remove loop which compares row i to row i-1


Hello, 

I've not been following this thread but this seems ndependent from 
previous posts. Try the following. 



url <- "http://r.789695.n4.nabble.com/file/n4636337/BR3_2011_New.csv"; 
tUnitsort <- read.csv(url, header=TRUE) 

cols <- sapply(c("Date", "Hour", "BlockNumber", "MyTo"), function(x) 
         grep(x, names(tUnitsort))) 

# This does it 
# Use full data.frame 'tUnitsort' if you want 
data.tmp <- aggregate(MyTo ~ Date + Hour, data = tUnitsort[, cols], max)
data.tmp <- merge(tUnitsort[, cols], data.tmp, by=c("Date", "Hour")) 

# Make it pretty 
idx <- grep("MyTo", names(data.tmp)) 
names(data.tmp)[idx] <- c("MyTo", "NewColumn") 

# See it 
head(data.tmp, 20) 
tail(data.tmp, 20) 


Also, you should quote the context. Many, almost all of us do NOT read 
the posts on Nabble. And Nabble does have a "quote" button. 

Hope this helps, 

Rui Barradas 

Em 12-07-2012 18:55, jcrosbie escreveu: 

> Thank you, 
> 
> I am sorry but I am still trying to figure out how to make the function 
> work. 
> 
> I have a column called tUnitsort$BlockNumber which can range from 0 to 6.
> I have another two columns with the date and the hour ending for the given 
> day. 
> Example 
> 
> Date        Hour   BlockNumber  MyTo  NewColumn 
> 2011-01   1          2                            140   
>   140 
> 2011-01   1          1                           70     
>    140 
> 2011-01   1          0                           0     
>       140 
> 2011-02   1          2                           160     
> 160 
> 2011-02   1          1                           70     
>    160 
> 2011-02   1          0                           0     
>      160 
> 2011-03   1          2                           150     
> 150 
> 
> I want to create a NewColumn which will place the MyTo number for the 
> highest block number for the rest blocks in a given hour ending within a
> day. 
> 
> 
> ifelse(tUnitsort[,4]>=tUnitsort[-1,4],tUnitsort[,7],tUnitsort[-1,7]) 
> 
> I am unsure how to refference the element before in this case.  I thought 
> the -1 was doing this but I believe I'm wrong now. 
> 
> http://r.789695.n4.nabble.com/file/n4636337/BR3_2011_New.csv
> BR3_2011_New.csv 
> 
> -- 
> View this message in context: 
> http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327p4636337.html
> Sent from the R help mailing list archive at Nabble.com. 
> 
> __ 
> [hidden email] mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code. 
> 
__ 
[hidden email] mailing list 
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code. 




If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327p4636372.html
 
To unsubscribe from remove loop which compares row i to row i-1, click here.
NAML

--
View this message in context: 
http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327p4636376.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] remove loop which compares row i to row i-1

2012-07-12 Thread jcrosbie
Thank you, 

I am sorry but I am still trying to figure out how to make the function
work. 

I have a column called tUnitsort$BlockNumber which can range from 0 to 6. 
I have another two columns with the date and the hour ending for the given
day. 
Example

DateHour   BlockNumber  MyTo  NewColumn
2011-01   1  2140 140
2011-01   1  1   70140  
2011-01   1  0   0   140   
2011-02   1  2   160 160
2011-02   1  1   70160
2011-02   1  0   0  160
2011-03   1  2   150 150

I want to create a NewColumn which will place the MyTo number for the
highest block number for the rest blocks in a given hour ending within a
day. 


ifelse(tUnitsort[,4]>=tUnitsort[-1,4],tUnitsort[,7],tUnitsort[-1,7]) 

I am unsure how to refference the element before in this case.  I thought
the -1 was doing this but I believe I'm wrong now. 

http://r.789695.n4.nabble.com/file/n4636337/BR3_2011_New.csv
BR3_2011_New.csv 

--
View this message in context: 
http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327p4636337.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] remove loop which compares row i to row i-1

2012-07-05 Thread jcrosbie
Thank you, 

I tired

 ifelse(tUnitsort[length(tUnitsort$Hour),4]>=tUnitsort[-1,4],(tempMC
=tUnitsort[length(tUnitsort$Hour),7]),tempMC )

But this doesn't seem to work. 

Where am I going wrong? 

--
View this message in context: 
http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327p4635566.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] remove loop which compares row i to row i-1

2012-07-03 Thread jcrosbie

I would like to remove a loop to speed up my code. 

I want to remove a loop which references the last row. 

In general I want to a remove a loop which looks something like this:
for 2 to number of rows in a matrix do{
if indextrow-1 is < currentIndexRow then do something. 
}


My R code:

for (i in 2:length(tUnitsort$Hour)){
  ifelse(tUnitsort[i,4]>=tUnitsort[i-1,4],(tempMC
=tUnitsort[i,7]),tempMC ) #col. 4 = BlockNumber; note tests to see if the
offers have change to the next set of blocks. 
  ifelse(tUnitsort[i,4]>=tUnitsort[i-1,4],(tempAC
=tUnitsort[i,7]-(tUnitsort[i,8]-tUnitsort[i,9])),tempAC )
  tUnitsort$MC[i] <- tempMC
  tUnitsort$AC[i] <- tempAC
  tUnitsort$PercentofMC[i] <- tUnitsort$Size[i]/tempMC
  tUnitsort$PercentofAC[i] <- tUnitsort$AvailableMW[i]/tempAC
}

--
View this message in context: 
http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] plot background - excel gradient style background ?

2012-06-29 Thread jcrosbie
I have a number of different figures I wish to create with a gradient
background. In addition to the two examples I've uploaded I need a boxplot,
histogram, etc. 


http://r.789695.n4.nabble.com/file/n4634932/fig1.png fig1.png 
http://r.789695.n4.nabble.com/file/n4634932/fig2.png fig2.png 

--
View this message in context: 
http://r.789695.n4.nabble.com/plot-background-excel-gradient-style-background-tp4632138p4634932.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Percent of a given subset

2012-06-08 Thread jcrosbie

How would I find the Percent of FuelTypeNum within the Band given 
AvailableMW?

example:
type 1 is 1%  of PB0 
type 2 is 54%  of PB0 
 type 4 is 4%  of PB0 
 type 5 is 42%  of PB0 


Note:   the Bands and fuel types are not always constant. 

Data:
FuelTypeNum Bands   AvailableMW AvailableMWNewFormat
1   PB0 185319  185.319
2   PB0 1835200018352
4   PB0 1338785 1338.785
5   PB0 1418975614189.756
2   PB1 48229   48.229
5   PB1 792433  792.433
2   PB2 7034959 7034.959
4   PB2 796 0.796
5   PB2 1652519 1652.519
1   PB3 16  0.016
2   PB3 11633   11.633
4   PB3 80768.076
5   PB3 97410   97.41
1   PB4 46104.61
2   PB4 30483.048
4   PB4 11171.117
5   PB4 28097   28.097
1   PB5 11551.155
2   PB5 53315.331
4   PB5 24912.491
5   PB5 12887   12.887
1   PB6 4   0.004
2   PB6 57745.774
4   PB6 26432.643
5   PB6 25475   25.475
1   PB7 133 0.133
2   PB7 37873.787
4   PB7 750 0.75
5   PB7 10682   10.682
http://r.789695.n4.nabble.com/file/n4632857/Rdata.xlsx Rdata.xlsx 

--
View this message in context: 
http://r.789695.n4.nabble.com/Percent-of-a-given-subset-tp4632857.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] ggplot incorrect legend

2012-06-08 Thread jcrosbie
Thank you, that was very helpful. 

Would it be possible to rename the legend values as well. Example 1 as
Biomass, 2 as coal, 4 as gas, 5 as hydro?

ggplot(data=tempTable, aes(x=Bands8, y=AvailableMWNewFormat,
fill=as.factor(FuelTypeNum))) +
  geom_bar(position="stack", stat="identity")+
  guides(fill = guide_legend(title = "Fuel Type", title.position = "top"))+
  xlab( "Price Bands")+
  ylab("Available MW") 

--
View this message in context: 
http://r.789695.n4.nabble.com/ggplot-incorrect-legend-tp4632471p4632843.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] flagging values without a loop

2012-06-07 Thread jcrosbie
For a given hour I want to be able to  add a new column called flag.  The
flag column will flag the highest price in a given hour.  Is there a way to
do this without a loop? 

matrix:
Unit,   Day,Hour,   Price,  Flag
afd11/2/20031   1   N
afd11/2/20031   2   N
afd11/2/20031   3   N
afd11/2/20031   4   Y
dcf11/2/20032   2   N
dcf11/2/20032   3   Y
dcf11/2/20032   1   N
dcf11/2/20032   2   N
dcf11/2/20032   3   Y
ghg21/2/20033   1   N
afd11/2/20033   2   N
.


--
View this message in context: 
http://r.789695.n4.nabble.com/flagging-values-without-a-loop-tp4632689.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] ggplot incorrect legend

2012-06-06 Thread jcrosbie
Thank you for your help. 

I would expect FuelTypeNum to make up the legend.  But in the legend there
is an extra value of called '3' and in the chart there is an extra
FuelTypeNum. 

code:
ggplot(data=tempTable, aes(x=Bands8, y=AvailableMWNewFormat,
fill=FuelTypeNum)) +
  geom_bar(position="stack", stat="identity") +
  coord_flip()

Plot
http://r.789695.n4.nabble.com/file/n4632552/Rplot.jpeg 

Data:
http://r.789695.n4.nabble.com/file/n4632552/dataRtest.csv dataRtest.csv 

--
View this message in context: 
http://r.789695.n4.nabble.com/ggplot-incorrect-legend-tp4632471p4632552.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] ggplot incorrect legend

2012-06-06 Thread jcrosbie
I'm expecting the legend to be 1,2,4,5,10 not 2,4,6,8,10. 

Is there away I can set my own colour and legend tittles? 

--
View this message in context: 
http://r.789695.n4.nabble.com/ggplot-incorrect-legend-tp4632471p4632529.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] ggplot incorrect legend

2012-06-05 Thread jcrosbie
How do I create a legend with ggplot?

I think should be getting the FuelTypeNum in the legend. 

Plot:
http://r.789695.n4.nabble.com/file/n4632471/Rplot.jpeg 

My code is:
ggplot(data=tempTable, aes(x=Bands8, y=SubPercent, fill=FuelTypeNum)) +
  geom_bar(position="stack", stat="identity") +
  scale_colour_hue('my legend', breaks = levels(tempTable$FuelTypeNum),
labels=c('Bio', 'Coal', 'Gas', 'Hydro','Other'))


tempTable:
   FuelTypeNum Bands8 AvailableMW AvailableMWNewFormat   SubPercent
11PB0  185351  185.351 4.355864e-03
22PB02599475325994.753 6.108928e-01
34PB0 1369528 1369.528 3.218476e-02
45PB01452252114522.521 3.412883e-01
5   10PB0  479915  479.915 1.127830e-02
62PB1   48229   48.229 5.736753e-02
75PB1  792473  792.473 9.426325e-01
82PB21179240211792.402 7.914544e-01
94PB2   27187   27.187 1.824672e-03
10   5PB2 2770769 2770.769 1.859619e-01
11  10PB2  309303  309.303 2.075906e-02
12   1PB3  480.048 3.983475e-05
13   2PB3  136844  136.844 1.135656e-01
14   4PB3   65079   65.079 5.400845e-02
15   5PB3  793226  793.226 6.582909e-01
16  10PB3  209781  209.781 1.740953e-01
17   1PB4   22113   22.113 4.340998e-02
18   2PB4   57843   57.843 1.135515e-01
19   4PB4   20315   20.315 3.988033e-02
20   5PB4  382714  382.714 7.513050e-01
21  10PB4   26414   26.414 5.185326e-02
22   1PB5   39173   39.173 3.232381e-02
23   2PB5  174913  174.913 1.443304e-01
24   4PB5  103542  103.542 8.543824e-02
25   5PB5  874764  874.764 7.218162e-01
26  10PB5   19501   19.501 1.609135e-02
27   1PB6   40.004 4.550429e-06
28   2PB6  175038  175.038 1.991245e-01
29   4PB6   91547   91.547 1.041445e-01
30   5PB6  610371  610.371 6.943625e-01
31  10PB620782.078 2.363948e-03
32   1PB7   36103   36.103 1.109041e-02
33   2PB7  210681  210.681 6.471866e-02
34   4PB7  739013  739.013 2.270159e-01
35   5PB7 2239824 2239.824 6.880469e-01

--
View this message in context: 
http://r.789695.n4.nabble.com/ggplot-incorrect-legend-tp4632471.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] plot background - excel gradient style background ?

2012-06-05 Thread jcrosbie
I'm not the one who is choosing to use gradient background. It's our company
policy.  

--
View this message in context: 
http://r.789695.n4.nabble.com/plot-background-excel-gradient-style-background-tp4632138p4632409.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] points() function will not plot

2012-06-01 Thread jcrosbie
I can not get the point function to work with in my code. 

The code:

   AggOfTempMatrix$CumSumPercentSize<-c(
 0.05265450, 0.05738490, 0.05865403, 0.05911553, 0.05957703, 0.06003854,
0.06058368,
 0.06098750, 0.06147208, 0.06187589, 0.06291427, 0.06331808, 0.06354884,
0.06408533,
 0.06489296, 0.07382014, 0.07397590, 0.08124456, 0.08228294, 0.0827,
0.08283097,
 0.08306172, 0.08349438, 0.08372513, 0.08412894, 0.08435969, 0.08453276,
0.08491350,
 0.08570382, 0.08599226, 0.08749791, 0.08831131, 0.08848437, 0.08954582,
0.09058420,
 0.09069958, 0.09112358, 0.09123896, 0.11087010, 0.11410062, 0.15309754,
0.20017364,
 1.)

AggOfTempMatrix$Price<-c(
0.00,  71.35,  72.00,  74.00,  75.00,  76.00,  84.24,  87.00, 88.00,  90.24, 
91.00,
94.22,  96.00,  98.00, 100.25, 121.00, 246.12, 272.44, 273.00, 295.00,
435.00, 455.14,
460.00, 475.00, 586.00, 591.00, 609.00, 622.00, 656.00, 750.00, 789.16,
798.00, 813.00,
834.00, 850.00, 895.00, 904.00, 915.00, 916.00, 924.00, 950.00, 996.66,
999.24)

plot(AggOfTempMatrix$Price, AggOfTempMatrix$CumSumPercentSize, type="l",
main = mainTitle, xlab=xAxislable, ylab=yAxislable, lwd=5)
points(x=0.5, y =300, col="darkred",pch=12) #this wont plot

--
View this message in context: 
http://r.789695.n4.nabble.com/points-function-will-not-plot-tp4632135.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] plot background - excel gradient style background ?

2012-06-01 Thread jcrosbie
Is there away of putting an excel style gradient background?  I want to have
dark blue in the middle and shad to white on the top and bottom.

--
View this message in context: 
http://r.789695.n4.nabble.com/plot-background-excel-gradient-style-background-tp4632138.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.