My other Question about "Add missing xts/zoo data with linear interpolation in R" you can find here Add missing xts/zoo data with linear interpolation in R .
But in general there is one more problem with my data - I do have "wrong" values not making sense:
"2012-04-09 05:03:00",2
"2012-04-09 05:04:00",4
"2012-04-09 05:05:39",-10
"2012-04-09 05:09:00",0
"2012-04-09 05:10:00",1
So the replacement of missing dates works:
y <- merge(y, zoo(,seq(start(y),end(y),by="min")), all=TRUE)
y <- na.approx(y)
But as you can see the -10 makes no sense, and the value is not at min:sec with value 00. I need a solution like na.rm. Thanks!
It is not clear what do you want to do. But I guess you want to remove some outliers from xts object. If you want a solution like "na.rm", one idea is to replace non desired values by NA
then you remove them using na.omit
.
x <- read.zoo(text='
"2012-04-09 05:03:00",2
"2012-04-09 05:04:00",4
"2012-04-09 05:05:39",-10
"2012-04-09 05:09:00",0
"2012-04-09 05:10:00",1',sep=',',tz='')
x[x == -10] <- NA
na.omit(x)
x
2012-04-09 05:03:00 2
2012-04-09 05:04:00 4
2012-04-09 05:09:00 0
2012-04-09 05:10:00 1
EDIT
To get condition per date , you can look at index(x) and format it for example.
format(index(dat),'%S')
[1] "00" "00" "39" "00" "00"
But here I use built-in .indexsec
( see also .indexmin, .indexhour,..)
dat[.indexsec(dat) != 0]
2012-04-09 05:05:39
-10
Here's how to replace value that are below a threshold (0 in this case) with NA. You can run na.approx
afterwards.
# read in
Lines <- '"2012-04-09 05:03:00",2
"2012-04-09 05:04:00",4
"2012-04-09 05:05:39",-10
"2012-04-09 05:09:00",0
"2012-04-09 05:10:00",1
'
data2 <- as.xts(read.zoo(text = Lines, sep = ",", tz = ""))
# perform calculation
data2[data2<0] <- NA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With