[R] annual maximum value

2009-02-16 Thread CJ Rubio

 hi everyone!

hope you can help me here.

i am a new R user. what i am trying to do is to find the maximum annual
discharge from a daily record. i have a data.frame which includes date and
the discharge. somewhat like this..

10/1/1989   2410
10/2/1989   2460
10/3/1989   2890
...
...
...
12/31/2005 5730

i have been browsing through the archives and fount out about the aggregate
function and the zoo package. here's one of the codes i've tried

DF <- read.table(data[i], sep =",")  ##i have several stations to assimilate
Date <- as.Date(as.character(DF[,3]), "%m/%d/%Y") #the date is at the 3rd
column, obviously
library(zoo)
z <- aggregate(zoo(DF[,4]), cut(Date, "y"), max)
max.discharge <- coredata(z)
date <- time(z)

the result should somehow look like this 
11/21/1926  32600
 4/24/1927   66500
...
...
..
4/26/2005   111000

thanks for your time reading my questions,, any suggestions will be truly
appreciated...
  


-- 
View this message in context: 
http://www.nabble.com/annual-maximum-value-tp22049205p22049205.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] annual maximum value

2009-02-16 Thread CJ Rubio

thank you very much for your reply.

i studied the code you gave me and tried to make adjustments to fit the
requirements that i need, thank you again.. i used the following codes:

> m <- read.table("D:/documents/5 stations/01014000.csv", sep =",")
> z <- zoo(m[,4],as.Date(as.character(DF[,3]), "%m/%d/%Y"))
> x <- aggregate(z, cut(time(z),"y"), max)

i got the maximum values right using coredata(), my question now is, how can
i call for the complete date (-mm-dd)  and the year () when the
maximum observation for that year was observed? 




CJ Rubio wrote:
> 
>  hi everyone!
> 
> hope you can help me here.
> 
> i am a new R user. what i am trying to do is to find the maximum annual
> discharge from a daily record. i have a data.frame which includes date and
> the discharge. somewhat like this..
> 
> 10/1/1989   2410
> 10/2/1989   2460
> 10/3/1989   2890
> ...
> ...
> ...
> 12/31/2005 5730
> 
> i have been browsing through the archives and fount out about the
> aggregate function and the zoo package. here's one of the codes i've tried
> 
> DF <- read.table(data[i], sep =",")  ##i have several stations to
> assimilate
> Date <- as.Date(as.character(DF[,3]), "%m/%d/%Y") #the date is at the 3rd
> column, obviously
> library(zoo)
> z <- aggregate(zoo(DF[,4]), cut(Date, "y"), max)
> max.discharge <- coredata(z)
> date <- time(z)
> 
> the result should somehow look like this 
> 11/21/1926  32600
>  4/24/1927   66500
> ...
> ...
> ..
> 4/26/2005   111000
> 
> thanks for your time reading my questions,, any suggestions will be truly
> appreciated...
>   
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/annual-maximum-value-tp22049205p22051398.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] problem with dates in zoo package

2009-02-17 Thread CJ Rubio

i have the following code - assimilating the maximum annual discharge each
year ffrom a daily discharge record from year 1989-2005.

m <- read.table("D:/documents/5 stations/01014000.csv", sep =",")
z <- zoo(m[,4],as.Date(as.character(m[,3]), "%m/%d/%Y")) 
x <- aggregate(z, floor(as.numeric(as.yearmon(time(z, max)   #code
suggested by Gabor Grothendieck

which gives me the maximum discharge each year and the year itself.. what i
am trying now is to produce the date when the maximum discharge was observed
in the pattern -mm-dd, like:

1988 11/9/1988  18600
1989  5/8/1989   49000
...
2005 4/26/2005111000 

thank you all in advance...
-- 
View this message in context: 
http://www.nabble.com/problem-with-dates-in-zoo-package-tp22053103p22053103.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] rbind: number of columns of result is not a multiple of vector length (arg 1)

2009-02-17 Thread CJ Rubio

i have the following constructed and running very well,, thanks to Gabor
Grothendieck for his help.

>data.info <- c("station.id", "year", "date", "max.discharge")
>
> for(i in 1:num.files) {
+ station.id <- substring(data[i], 1,8)
+ DF <- read.table(data[i], sep=",", blank.lines.skip = TRUE)
+ z <- zoo(DF[,4], as.Date(as.character(DF[,3]), "%m/%d/%Y"))
+ f <- function(x) time(x) [which.max(x)]
+ ix <- tapply(z, floor(as.yearmon(time(z))),f)
+ year <- (1988:2005)
+ date <- time(z[ix])
+ max.discharge <- coredata(z[ix])
+ data.info <- rbind(data.info, c(station.id, year, date, max.discharge))
+ }

my problem with my code occurs in the part where I arrange my results..
after running this code, i get this warning:

Warning message:
In rbind(data.info, c(station.id, year, date, max.discharge)) :
  number of columns of result is not a multiple of vector length (arg 1)


i can't figure out what to do to produce the result i wanted:
(for each station, it should look like this:)

data.info "station.id" "year"   "date"  "max.discharge" 
   "01014000" 1988   "1988-11-07"   4360 
   "01014000" 1989   "1989-05-13" 2  
   "01014000" 1990   "1990-10-25"   9170
   "01014000" 1991   "1991-04-22" 12200
   "01014000" 1992   "1992-03-29" 11800

   "01014000" 2005   "2005-04-04" 22100

thanks in advence for your help..
-- 
View this message in context: 
http://www.nabble.com/rbind%3A-number-of-columns-of-result-is-not-a-multiple-of-vector-length-%28arg-1%29-tp22070942p22070942.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] rbind: number of columns of result is not a multiple of vector length (arg 1)

2009-02-17 Thread CJ Rubio

it works perfectly thank you for your help

what if i want to seperate each stations data and save in a .csv file??
while i'm asking you these i'm also finding some ways to do so..

thank you again.





jholtman wrote:
> 
> try using:
> 
> data.info <- rbind(data.info, cbind(station.id, year, date,
> max.discharge))
> 
> On Tue, Feb 17, 2009 at 9:26 PM, CJ Rubio  wrote:
>>
>> i have the following constructed and running very well,, thanks to Gabor
>> Grothendieck for his help.
>>
>>>data.info <- c("station.id", "year", "date", "max.discharge")
>>>
>>> for(i in 1:num.files) {
>> + station.id <- substring(data[i], 1,8)
>> + DF <- read.table(data[i], sep=",", blank.lines.skip = TRUE)
>> + z <- zoo(DF[,4], as.Date(as.character(DF[,3]), "%m/%d/%Y"))
>> + f <- function(x) time(x) [which.max(x)]
>> + ix <- tapply(z, floor(as.yearmon(time(z))),f)
>> + year <- (1988:2005)
>> + date <- time(z[ix])
>> + max.discharge <- coredata(z[ix])
>> + data.info <- rbind(data.info, c(station.id, year, date, max.discharge))
>> + }
>>
>> my problem with my code occurs in the part where I arrange my results..
>> after running this code, i get this warning:
>>
>> Warning message:
>> In rbind(data.info, c(station.id, year, date, max.discharge)) :
>>  number of columns of result is not a multiple of vector length (arg 1)
>>
>>
>> i can't figure out what to do to produce the result i wanted:
>> (for each station, it should look like this:)
>>
>> data.info "station.id" "year"   "date"  "max.discharge"
>>   "01014000" 1988   "1988-11-07"   4360
>>   "01014000" 1989   "1989-05-13" 2
>>   "01014000" 1990   "1990-10-25"   9170
>>   "01014000" 1991   "1991-04-22" 12200
>>   "01014000" 1992   "1992-03-29" 11800
>>
>>   "01014000" 2005   "2005-04-04" 22100
>>
>> thanks in advence for your help..
>> --
>> View this message in context:
>> http://www.nabble.com/rbind%3A-number-of-columns-of-result-is-not-a-multiple-of-vector-length-%28arg-1%29-tp22070942p22070942.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.
>>
> 
> 
> 
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
> 
> What is the problem that you are trying to solve?
> 
> __
> 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.
> 
> 

-- 
View this message in context: 
http://www.nabble.com/rbind%3A-number-of-columns-of-result-is-not-a-multiple-of-vector-length-%28arg-1%29-tp22070942p22071324.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] basic inquiry regarding write.csv

2009-02-17 Thread CJ Rubio

i have a loop which looks likes this:


> data.info <- rbind(data.info, cbind(station.id, year, date,
> max.discharge))
+  y <- split(data.info, data.info[station.id])
+  for (i in names(y))
 {write.csv(y[[i]], file=paste(i, ".csv", sep=","))}

i am wondering, where the file (which i am about to write in .csv format)
will be saved? i looked at ?write.csv and it says there that :

file   either a character string naming a file or a
connection open for writing. "" indicates output to the console. 

correct me if i'm wrong but the way i undestand it is, i should have a file
or a working directory where the .csv will be written.  if for example i
have a working directory "E:/my_work_directory", how can i save this
splitted files in the same directory?

can anybody please enlighten me more with write.csv and the argument file?

thanks..

-- 
View this message in context: 
http://www.nabble.com/basic-inquiry-regarding-write.csv-tp22073053p22073053.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] basic inquiry regarding write.csv

2009-02-17 Thread CJ Rubio

thanks for your reply.. is there something wrong with the code i have?
because it doesn't write the file in the directory that i am using...

for (i in names(y))
> {write.csv(y[[i]], file=paste(i, ".csv", sep=","))}

thanks again..



ronggui-2 wrote:
> 
> If the "file" is a relative path, then it should be in the working
> directory. Say, the working directory is E:/my_work_directory (of
> course, you can get it by getwd()), and you export a data frame "a" to
> csv by:
> write.csv(a, file="a.csv"), then the file should be
> "E:/my_work_directory/a.csv".
> 
> Best
> 
> 2009/2/18 CJ Rubio :
>>
>> i have a loop which looks likes this:
>>
>>
>>> data.info <- rbind(data.info, cbind(station.id, year, date,
>>> max.discharge))
>> +  y <- split(data.info, data.info[station.id])
>> +  for (i in names(y))
>> {write.csv(y[[i]], file=paste(i, ".csv", sep=","))}
>>
>> i am wondering, where the file (which i am about to write in .csv format)
>> will be saved? i looked at ?write.csv and it says there that :
>>
>> file   either a character string naming a file or a
>> connection open for writing. "" indicates output to the console.
>>
>> correct me if i'm wrong but the way i undestand it is, i should have a
>> file
>> or a working directory where the .csv will be written.  if for example i
>> have a working directory "E:/my_work_directory", how can i save this
>> splitted files in the same directory?
>>
>> can anybody please enlighten me more with write.csv and the argument
>> file?
>>
>> thanks..
>>
>> --
>> View this message in context:
>> http://www.nabble.com/basic-inquiry-regarding-write.csv-tp22073053p22073053.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.
>>
> 
> 
> 
> -- 
> HUANG Ronggui, Wincent
> Tel: (00852) 3442 3832
> PhD Candidate
> Dept of Public and Social Administration
> City University of Hong Kong
> Homepage: http://ronggui.huang.googlepages.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.
> 
> 

-- 
View this message in context: 
http://www.nabble.com/basic-inquiry-regarding-write.csv-tp22073053p22073393.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] basic inquiry regarding write.csv

2009-02-17 Thread CJ Rubio

this is the code that i have so far:

> data.path <- file.path ("D:/documents/research/5 stations")
> setwd(data.path)
> getwd()

> data <- dir(".")
> num.files <- length(data)

>  for(i in 1:num.files) {
+  station.id <- substring(data[i], 1,8)
+  DF <- read.table(data[i], sep=",", blank.lines.skip = TRUE)
+  z <- zoo(DF[,4], as.Date(as.character(DF[,3]), "%m/%d/%Y"))
+  f <- function(x) time(x) [which.max(x)]
+  ix <- tapply(z, floor(as.yearmon(time(z))),f)
+  year <- (1988:2005)
+  date <- time(z[ix])
+  max.discharge <- coredata(z[ix])
+  data.info <- rbind(data.info, cbind(station.id, year, date,
max.discharge))
+  y <- split(data.info, data.info[station.id])
+  for (i in names(y)) {write.csv(y[[i]], file=paste(i, ".csv", sep=","))}
+ }

i have done the suggestion you gave me..  and there were no errors when i
run it.
hope this would further clarify my question.



So far, I cannot see any mistake, though the sep="" will be more
elegant that sep=",". Are you sure your working directory is
"E:/my_work_directory"? and is there any error msg?

BTW, a reproducible example will help to get better response from the list.

2009/2/18 CJ Rubio :
>
> thanks for your reply.. is there something wrong with the code i have?
> because it doesn't write the file in the directory that i am using...
>
> for (i in names(y))
>> {write.csv(y[[i]], file=paste(i, ".csv", sep=","))}
>
> thanks again..
>
>
>
> ronggui-2 wrote:
>>
>> If the "file" is a relative path, then it should be in the working
>> directory. Say, the working directory is E:/my_work_directory (of
>> course, you can get it by getwd()), and you export a data frame "a" to
>> csv by:
>> write.csv(a, file="a.csv"), then the file should be
>> "E:/my_work_directory/a.csv".
>>
>> Best
>>
>> 2009/2/18 CJ Rubio :
>>>
>>> i have a loop which looks likes this:
>>>
>>>
>>>> data.info <- rbind(data.info, cbind(station.id, year, date,
>>>> max.discharge))
>>> +  y <- split(data.info, data.info[station.id])
>>> +  for (i in names(y))
>>> {write.csv(y[[i]], file=paste(i, ".csv", sep=","))}
>>>
>>> i am wondering, where the file (which i am about to write in .csv
>>> format)
>>> will be saved? i looked at ?write.csv and it says there that :
>>>
>>> file   either a character string naming a file or a
>>> connection open for writing. "" indicates output to the console.
>>>
>>> correct me if i'm wrong but the way i undestand it is, i should have a
>>> file
>>> or a working directory where the .csv will be written.  if for example i
>>> have a working directory "E:/my_work_directory", how can i save this
>>> splitted files in the same directory?
>>>
>>> can anybody please enlighten me more with write.csv and the argument
>>> file?
>>>
>>> thanks..
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/basic-inquiry-regarding-write.csv-tp22073053p22073053.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.
>>>
>>
>>
>>
>> --
>> HUANG Ronggui, Wincent
>> Tel: (00852) 3442 3832
>> PhD Candidate
>> Dept of Public and Social Administration
>> City University of Hong Kong
>> Homepage: http://ronggui.huang.googlepages.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.
>>
>>
>
> --
> View this message in context:
> http://www.nabble.com/basic-inquiry-regarding-write.csv-tp22073053p22073393.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://