Hi, I have the following code and am using R 2.7.2: JIBqq <- (Jqrt.IB[2:length(Jqrt.IB)]/Jqrt.IB[1:(length(Jqrt.IB)-1)])-1 JIBqq <- cbind(zoo(JIBqq, Jqrt.time[2:length(Jqrt.time)])) y.JIBqq <- merge(JIBqq,lag(JIBqq,-1))> y.JIBqqJIBqq lag(JIBqq, -1) 2004-06-30 0.003318909 NA 2004-09-30 -0.028536136 0.003318909 2004-12-31 0.011529924 -0.028536136 2005-03-31 0.066068689 0.011529924 2005-06-30 0.023462638 0.066068689 2005-09-30 -0.006443928 0.023462638 2005-12-31 -0.030541853 -0.006443928 2006-03-31 -0.057128956 -0.030541853 2006-06-30 -0.014534821 -0.057128956 2006-09-30 -0.030611132 -0.014534821 2006-12-31 0.020189061 -0.030611132 2007-03-31 0.009045381 0.020189061 2007-06-30 0.025034436 0.009045381 2007-09-30 0.032251184 0.025034436 2007-12-31 0.051555978 0.032251184 2008-03-31 0.001866808 0.051555978 2008-06-30 0.043680370 0.001866808 2008-09-30 0.031271730 0.043680370 y.JIBqq[,1][y.JIBqq[,1]> y.JIBqq[,2]]<-'up move' y.JIBqq[,1][y.JIBqq[,1]< y.JIBqq[,2]]<-'down move'> y.JIBqqJIBqq lag(JIBqq, -1) 2004-06-30 0.00331890938117052 <NA> 2004-09-30 down move 0.00331890938117052 2004-12-31 up move -0.0285361360020099 2005-03-31 up move 0.0115299239240534 2005-06-30 down move 0.0660686892790183 2005-09-30 down move 0.0234626376481404 2005-12-31 -0.0305418525579293 -0.0064439280623304 2006-03-31 -0.0571289557443185 -0.0305418525579293 2006-06-30 up move -0.0571289557443185 2006-09-30 -0.0306111317173215 -0.0145348212173988 2006-12-31 up move -0.0306111317173215 2007-03-31 down move 0.0201890614449696 2007-06-30 up move 0.00904538134004018 2007-09-30 up move 0.0250344360146053 2007-12-31 up move 0.0322511838232158 2008-03-31 down move 0.0515559780138033 2008-06-30 up move 0.00186680815107398 2008-09-30 down move 0.0436803703122548 My question is why 2005-12-31, 2006-03-31, and 2006-09-30 do not take the assigned character? I have done this same procedure with several other data and have not run into this problem. Any help is appreciated. Thanks, Morgan K. Boyce GPS - Quant?Finance Analyst Phone: 980-386-4074
Boyce, Morgan <morgan.boyce <at> bankofamerica.com> writes:>> y.JIBqq[,1][y.JIBqq[,1]> y.JIBqq[,2]]<-'up move' > y.JIBqq[,1][y.JIBqq[,1]< y.JIBqq[,2]]<-'down move'Your first command above makes y.JIBqq[,1] into a character vector (rather than numeric), which then makes the subsequent comparison complicated/tricky (it will be a lexical, rather than a numeric one -- for example, consider the fact that "-0.01">"0.001" is TRUE) A better way to do what you want is probably something like ## read in data x <- read.table("miscdate.txt", colClasses=c("Date","numeric","numeric")) rownames(x) <- as.character(x[,1]) x <- x[,-1] ## at this point I have something like your y.JIBqq move <- ifelse(x[,1]>x[,2],'up move','down move') x <- data.frame(x,move)
Boyce, Morgan <morgan.boyce <at> bankofamerica.com> writes: PS: it would be a good idea to use a more informative subject line (say, something like "problem with comparison in data frames"). Most of the e-mail that comes to the R help list is about "R Help" (oddly enough) ... Ben Bolker
You can't mix character and numeric in a zoo object. Also note that lag.zoo can be used so you don't have to construct the lag yourself. library(zoo) set.seed(1) z <- zoo(rnorm(10), as.Date(1:10)) # these are the same except upmove is logical series # upmovenum is numeric series and move is a factor series upmove <- z > lag(z, -1) # TRUE if upmove upmovenum <- upmove + 0 move <- zoo(factor(upmove, lab = c("down", "up")), time(upmove)) plot(merge(z, upmove, upmovenum, move)) On Tue, Oct 21, 2008 at 3:21 PM, Boyce, Morgan <morgan.boyce at bankofamerica.com> wrote:> Hi, I have the following code and am using R 2.7.2: > > JIBqq <- (Jqrt.IB[2:length(Jqrt.IB)]/Jqrt.IB[1:(length(Jqrt.IB)-1)])-1 > JIBqq <- cbind(zoo(JIBqq, Jqrt.time[2:length(Jqrt.time)])) > y.JIBqq <- merge(JIBqq,lag(JIBqq,-1)) > >> y.JIBqq > JIBqq lag(JIBqq, -1) > 2004-06-30 0.003318909 NA > 2004-09-30 -0.028536136 0.003318909 > 2004-12-31 0.011529924 -0.028536136 > 2005-03-31 0.066068689 0.011529924 > 2005-06-30 0.023462638 0.066068689 > 2005-09-30 -0.006443928 0.023462638 > 2005-12-31 -0.030541853 -0.006443928 > 2006-03-31 -0.057128956 -0.030541853 > 2006-06-30 -0.014534821 -0.057128956 > 2006-09-30 -0.030611132 -0.014534821 > 2006-12-31 0.020189061 -0.030611132 > 2007-03-31 0.009045381 0.020189061 > 2007-06-30 0.025034436 0.009045381 > 2007-09-30 0.032251184 0.025034436 > 2007-12-31 0.051555978 0.032251184 > 2008-03-31 0.001866808 0.051555978 > 2008-06-30 0.043680370 0.001866808 > 2008-09-30 0.031271730 0.043680370 > > y.JIBqq[,1][y.JIBqq[,1]> y.JIBqq[,2]]<-'up move' > y.JIBqq[,1][y.JIBqq[,1]< y.JIBqq[,2]]<-'down move' > >> y.JIBqq > JIBqq lag(JIBqq, -1) > 2004-06-30 0.00331890938117052 <NA> > 2004-09-30 down move 0.00331890938117052 > 2004-12-31 up move -0.0285361360020099 > 2005-03-31 up move 0.0115299239240534 > 2005-06-30 down move 0.0660686892790183 > 2005-09-30 down move 0.0234626376481404 > 2005-12-31 -0.0305418525579293 -0.0064439280623304 > 2006-03-31 -0.0571289557443185 -0.0305418525579293 > 2006-06-30 up move -0.0571289557443185 > 2006-09-30 -0.0306111317173215 -0.0145348212173988 > 2006-12-31 up move -0.0306111317173215 > 2007-03-31 down move 0.0201890614449696 > 2007-06-30 up move 0.00904538134004018 > 2007-09-30 up move 0.0250344360146053 > 2007-12-31 up move 0.0322511838232158 > 2008-03-31 down move 0.0515559780138033 > 2008-06-30 up move 0.00186680815107398 > 2008-09-30 down move 0.0436803703122548 > > My question is why 2005-12-31, 2006-03-31, and 2006-09-30 do not take the assigned character? I have done this same procedure with several other data and have not run into this problem. Any help is appreciated. > > Thanks, > > > Morgan K. Boyce > GPS - Quant Finance Analyst > Phone: 980-386-4074 > > ______________________________________________ > R-help at 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. >