Re: [R] Condition warning: has length > 1 and only the first element will be used

2008-10-15 Thread Joe Kaser
Ah! Thanks.
 ifelse() appears to do the job.
note: subtracting 12 from chron rather than noon doesn't seem to work.  I
think (but am not totally sure) chron interprets 12 as fraction of a day -
i.e. 12/1 days...


On Wed, Oct 15, 2008 at 1:33 PM, Greg Snow <[EMAIL PROTECTED]> wrote:

> The if statement is for program flow control (do this bunch of code if this
> condition is true, else do this), the vectorized version is the ifelse
> function, that is the one that you want to use.
>
> Also note (this is for times in general, not sure about chron specifically)
> subtracting noon from the times gives you time differences, subtracting a
> time difference from a time will give you another time, so it may be simpler
> to subtract just 12, rather than noon from your times.
>
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> [EMAIL PROTECTED]
> 801.408.8111
>
>
> > -Original Message-
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> > project.org] On Behalf Of Joe Kaser
> > Sent: Wednesday, October 15, 2008 2:19 PM
> > To: r-help@r-project.org
> > Subject: [R] Condition warning: has length > 1 and only the first
> > element will be used
> >
> > Hello,
> >
> > I've been learning R functions recently and I've come across a problem
> > that
> > perhaps someone could help me with.
> >
> > # I have a a chron() object of times
> >
> > > hours=chron(time=c("01:00:00","18:00:00","13:00:00","10:00:00"))
> >
> > # I would like to subtract 12 hours from each time element, so I
> > created a
> > vector of 12 hours (actually, more like a vector of noons)
> >
> > > less.hours=chron(time=rep("12:00:00",4))
> >
> > # If I just subtract the two vectors, I get some negative values, as
> > fractions of a day
> >
> > > hours-less.hours
> > [1] -0.4583  0.2500  0.0417 -0.0833
> >
> > # I would like those negative values to cycle around and subtract the
> > amount
> > < 0 from midnight
> > # That is to say, 01:00:00 - 12:00:00 should be 13:00:00
> > # because 01:00:00-12:00:00 = -11:00:00, and 24:00:00-11:00:00 =
> > 13:00:00
> > # It's sort of like going back to the previous day, but without
> > actually
> > including information about which day it is
> >
> > # This is what I tried
> >
> > > test.function=function(x,y) {
> > + sub = x-y
> > + if(sub<0) x+y
> > + }
> >
> > > test.function(hours,less.hours)
> > Time in days:
> > [1] 0.5416667 1.250 1.0416667 0.917
> > Warning message:
> > In if (sub < 0) x + y :
> >   the condition has length > 1 and only the first element will be used
> >
> >
> > # My questions are, why does it only use the first element?? Why does
> > it not
> > apply to all? Also, what happened to the elements where sub>= 0, it
> > looks
> > like they followed the rules
> > # of if(sub<0).  I feel like I must not be understanding something
> > rather
> > basic about how logical expressions operate within R
> > # Help would be appreciated...
> >
> > [[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]]

__
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] Condition warning: has length > 1 and only the first element will be used

2008-10-15 Thread Joe Kaser
Thanks for the help. ifelse does the job.
Could you elaborate, or give an example of the "awkward" things ifelse might
do to classed objects?

On Wed, Oct 15, 2008 at 1:38 PM, Peter Dalgaard <[EMAIL PROTECTED]>wrote:

> Joe Kaser wrote:
>
>> Hello,
>>
>> I've been learning R functions recently and I've come across a problem
>> that
>> perhaps someone could help me with.
>>
>> # I have a a chron() object of times
>>
>>  hours=chron(time=c("01:00:00","18:00:00","13:00:00","10:00:00"))
>>>
>>
>> # I would like to subtract 12 hours from each time element, so I created a
>> vector of 12 hours (actually, more like a vector of noons)
>>
>>  less.hours=chron(time=rep("12:00:00",4))
>>>
>>
>> # If I just subtract the two vectors, I get some negative values, as
>> fractions of a day
>>
>>  hours-less.hours
>>>
>> [1] -0.4583  0.2500  0.0417 -0.0833
>>
>> # I would like those negative values to cycle around and subtract the
>> amount
>> < 0 from midnight
>> # That is to say, 01:00:00 - 12:00:00 should be 13:00:00
>> # because 01:00:00-12:00:00 = -11:00:00, and 24:00:00-11:00:00 = 13:00:00
>> # It's sort of like going back to the previous day, but without actually
>> including information about which day it is
>>
>> # This is what I tried
>>
>>  test.function=function(x,y) {
>>>
>> + sub = x-y
>> + if(sub<0) x+y
>> + }
>>
>>  test.function(hours,less.hours)
>>>
>> Time in days:
>> [1] 0.5416667 1.250 1.0416667 0.917
>> Warning message:
>> In if (sub < 0) x + y :
>>  the condition has length > 1 and only the first element will be used
>>
>>
>> # My questions are, why does it only use the first element?? Why does it
>> not
>> apply to all? Also, what happened to the elements where sub>= 0, it looks
>> like they followed the rules
>> # of if(sub<0).  I feel like I must not be understanding something rather
>> basic about how logical expressions operate within R
>> # Help would be appreciated...
>>
>
> OK: help(ifelse), ordinary if doesn't vectorize.
>
> Actually, ifelse does awkward things with classed objects. You might want
> something like
>
> sub <- x - y
> sub[sub<0] <- x+y
>
> instead. And don't forget to return sub in all cases.
>
> --
>   O__   Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B
>  c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
>  (*) \(*) -- University of Copenhagen   Denmark  Ph:  (+45) 35327918
> ~~ - ([EMAIL PROTECTED])  FAX: (+45) 35327907
>

[[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] Condition warning: has length > 1 and only the first element will be used

2008-10-15 Thread Joe Kaser
Hello,

I've been learning R functions recently and I've come across a problem that
perhaps someone could help me with.

# I have a a chron() object of times

> hours=chron(time=c("01:00:00","18:00:00","13:00:00","10:00:00"))

# I would like to subtract 12 hours from each time element, so I created a
vector of 12 hours (actually, more like a vector of noons)

> less.hours=chron(time=rep("12:00:00",4))

# If I just subtract the two vectors, I get some negative values, as
fractions of a day

> hours-less.hours
[1] -0.4583  0.2500  0.0417 -0.0833

# I would like those negative values to cycle around and subtract the amount
< 0 from midnight
# That is to say, 01:00:00 - 12:00:00 should be 13:00:00
# because 01:00:00-12:00:00 = -11:00:00, and 24:00:00-11:00:00 = 13:00:00
# It's sort of like going back to the previous day, but without actually
including information about which day it is

# This is what I tried

> test.function=function(x,y) {
+ sub = x-y
+ if(sub<0) x+y
+ }

> test.function(hours,less.hours)
Time in days:
[1] 0.5416667 1.250 1.0416667 0.917
Warning message:
In if (sub < 0) x + y :
  the condition has length > 1 and only the first element will be used


# My questions are, why does it only use the first element?? Why does it not
apply to all? Also, what happened to the elements where sub>= 0, it looks
like they followed the rules
# of if(sub<0).  I feel like I must not be understanding something rather
basic about how logical expressions operate within R
# Help would be appreciated...

[[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] Ragged time series data

2008-10-04 Thread Joe Kaser
Thanks Stephen and Gabor. The zoo package is what I was looking for.

On Fri, Oct 3, 2008 at 5:44 PM, Gabor Grothendieck
<[EMAIL PROTECTED]>wrote:

> Check out the zoo package and its three vignettes and
> ?aggregate.zoo in particular.
>
> Also have a look at the article on dates and times in R News 4/1
> and note the chron class which, in fact, accepts inputs in the
> very form you have.
>
> On Fri, Oct 3, 2008 at 7:39 PM, Joe Kaser <[EMAIL PROTECTED]> wrote:
> > Hi and thanks in advance,
> >
> > I am fairly new with R so I hope this problem isn't too amateur.
> >
> > I have a vector of count data which correspond to vectors of date
> (%m/%d/%Y)
> > and time of day (%H:%M:%S).
> > I am trying to compute various statistics (e.g. daily max) by lumping the
> > data together by day.   I have been able to utilize tapply() and group
> the
> > counts together, but with the method I use I end up losing the
> corresponding
> > time of day information.
> >
> > This is what I have done so far
> >
> > data=*vector of integers
> > *hours= *"character" vector of times %H:%M:S% *
> > days= *"character" vector of dates %m/%d/%Y
> > *
> > hr.days=paste(days,hours)
> > hr.dayslt=as.POSIXlt(strptime(hr.days, format="%m/%d/%Y %H:%M:%S"))
> >
> > tapply(data,hr.dayslt$yday,max)
> >
> > This works to give me the counts I want corresponding to a Julain day,
> but
> > it does not tell me which hour corresponds to each daily maximum.  Is
> > tapply() the wrong route?
> >
> > Joe
> > *
> >
> >
> > *
> >
> >[[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]]

__
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] Ragged time series data

2008-10-03 Thread Joe Kaser
Hi and thanks in advance,

I am fairly new with R so I hope this problem isn't too amateur.

I have a vector of count data which correspond to vectors of date (%m/%d/%Y)
and time of day (%H:%M:%S).
I am trying to compute various statistics (e.g. daily max) by lumping the
data together by day.   I have been able to utilize tapply() and group the
counts together, but with the method I use I end up losing the corresponding
time of day information.

This is what I have done so far

data=*vector of integers
*hours= *"character" vector of times %H:%M:S% *
days= *"character" vector of dates %m/%d/%Y
*
hr.days=paste(days,hours)
hr.dayslt=as.POSIXlt(strptime(hr.days, format="%m/%d/%Y %H:%M:%S"))

tapply(data,hr.dayslt$yday,max)

This works to give me the counts I want corresponding to a Julain day, but
it does not tell me which hour corresponds to each daily maximum.  Is
tapply() the wrong route?

Joe
*


*

[[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.