Re: [R] Error on using lag function

2010-01-29 Thread anna
thanks Berend, that's a very smart to do it :) - Anna Lippel -- View this message in context: http://n4.nabble.com/Error-on-using-lag-function-tp1399935p1415584.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org

Re: [R] Error on using lag function

2010-01-29 Thread Berend Hasselman
anna wrote: > > Hello everyone, I have a vector P and I want to replace each of its > missing values by its next element, for example: > P[i] = NA --> P[i] = P[i+1] > You can also try P[which(is.na(P))]<- P[which(is.na(P))+1] or avoiding duplicate calculations index.Pna<-which(is.na(P)) P[i

Re: [R] Error on using lag function

2010-01-29 Thread anna
Peter, I decided to directly remove the NA's but when I tested it yes I wrote fromLast = TRUE so I might have checked erroneously sorry and thanks again I am going to keep this in mind because I might need it at some point, I have many cases where I need to handle missing values differently.

Re: [R] Error on using lag function

2010-01-28 Thread Peter Ehlers
Did you try it? I said library(zoo) na.locf(P, fromLast=TRUE) Note the 'fromLast=TRUE'; that tells na.locf() to use the reverse of your vector. What I meant by "You'll have to decide what to do if the last value is NA" is that you'll need to decide what to do if your vector ends with a NA, say

Re: [R] Error on using lag function

2010-01-28 Thread anna
Hi Peter, thank you for helping. The thing is don't want to it replace it with the last value but with the next value - Anna Lippel -- View this message in context: http://n4.nabble.com/Error-on-using-lag-function-tp1399935p1401319.html Sent from the R help mailing list archive at Nabble.co

Re: [R] Error on using lag function

2010-01-28 Thread Peter Ehlers
Does this help: library(zoo) na.locf(P, fromLast=TRUE) You'll have to decide what to do if the last value is NA. -Peter Ehlers anna wrote: Hello everyone, I have a vector P and I want to replace each of its missing values by its next element, for example: P[i] = NA --> P[i] = P[i+1] To do th

[R] Error on using lag function

2010-01-28 Thread anna
Hello everyone, I have a vector P and I want to replace each of its missing values by its next element, for example: P[i] = NA --> P[i] = P[i+1] To do this I am using the replace() and lag() functions like this: P <- replace(as.ts(P),is.na(as.ts(P)),as.ts(lag(P,1))) but here is the error that I ge