Re: [R] Condition warning: has length > 1 and only the first element will be used
Joe Kaser wrote: 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? It strips the class. One thing that usually gets me is this: > dd <- as.Date(c("2008-1-2","2007-3-21")) > ifelse(dd>as.Date("2008-1-1"),dd+7,dd) [1] 13887 13593 > dd [1] "2008-01-02" "2007-03-21" > dd+7 [1] "2008-01-09" "2007-03-28" (Actually, the result gets the attributes of the _condition_, which is usually no help at all.) -- 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 __ 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
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
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.
Re: [R] Condition warning: has length > 1 and only the first element will be used
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 __ 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
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. __ 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
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.