Hi:
I'm guessing that your time variable is not defined with a class that
respects (time) ordering, so your first if statement is meaningless. In the
absence of further information, either create a meaningful time/date
variable or at the very least, an ordered factor.
Given what you've provided, the best one can ascertain is that time is a(n
unordered) factor:
> d <- read.table(textConnection("
+ day time price
+ 1 1am 5
+ 1 2am 7
+ 1 3am 9
+ 1 4am 12
+ 2 1am 5
+ 2 2am 7
+ 2 3am 9
+ 2 4am 12"), header = TRUE)> str(d)
'data.frame': 8 obs. of 3 variables:
$ day : int 1 1 1 1 2 2 2 2
$ time : Factor w/ 4 levels "1am","2am","3am",..:
1 2 3 4 1 2 3 4
$ price: int 5 7 9 12 5 7 9 12
This is what I (or anyone else on the list) would see in an R session.
Whether this is reflective of what you see on your end is another matter. My
response above was conditioned on the observation that time is an unordered
factor, and logical testing based on order will not work with an unordered
factor.
It would be easier to diagnose the problem if you provided your data in the
following form:> dput(d)
structure(list(day = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), time structure(c(1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("1am", "2am",
"3am",
"4am"), class = "factor"), price = c(5L, 7L, 9L, 12L, 5L,
7L,
9L, 12L)), .Names = c("day", "time", "price"),
class = "data.frame",
row.names = c(NA,
-8L))
and copy/paste this into your help request (as suggested in the Posting
Guide, BTW).
dput() outputs not merely the data, but its structure - in particular, the
class of each variable. When you provide data in this form, there is no
ambiguity between what you see and what potential helpers will see in their
R sessions, so problem diagnosis is accomplished more efficiently.
To create meaningful time variables, look into ?DateTimeClasses or package
chron for starters.
HTH,
Dennis
On Fri, Jul 16, 2010 at 10:00 AM, George Coyle <gcoyle4@gmail.com> wrote:
> Hello,
>
> I am trying to find a direct way to write a nested if of sorts to find data
> for a specific time range for a specific day (across a range of days) and
> have exhausted my abilities with the manuals I have at hand. I have a good
> deal of data of this approximate form:
>
> day time price
> 1 1am 5
> 1 2am 7
> 1 3am 9
> 1 4am 12
> 2 1am 5
> 2 2am 7
> 2 3am 9
> 2 4am 12
> etc
>
> I am reading from a file I loaded and using a while statement. I then am
> trying the direct approach of writing the if:
>
> if(time>=2am &time <=4am) { #specifying desired time range--since
a while
> loop format I don't think date is necessary and if it is I don't
know how
> to
> say this?
> time1<-time #sets first time in desired interval to base time
> px1<-px #sets first price in desired interval to base price
> if(px>px1){ #nested if stmt to see if subsequent times are higher than
> base,
> thus replacing base with new max (I am seeking max in the time period in
> this instance)
> px2<-px #px2 would be the new higher maximum
> time2<-time #time associated with px2
> px3<-px+1 #price immediately following max
> time3<-time+1 #time immed. follows max
> out<-(cur_date,px2,time2,px3,time3) #output the high price/time and the
> immediately following price/time
> cat(out,"\n")}
>
> This code however, does nothing. Any help would be apreciated. The
> manuals
> seem to only take one so far.
>
> Thanks,
>
> George
>
> [[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.
>
[[alternative HTML version deleted]]