Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete specific values in R with zoo/xts

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!

like image 577
Herr Student Avatar asked Feb 16 '23 19:02

Herr Student


2 Answers

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
like image 115
agstudy Avatar answered Feb 19 '23 22:02

agstudy


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
like image 30
Pierre Lapointe Avatar answered Feb 20 '23 00:02

Pierre Lapointe