Reputation: 355
I merged 2 different (irregular) time series (merge.zoo() ). Looks like:
quir schme
2006-01-01 01:15:00 725.24 4.64
2006-01-01 01:30:00 725.24 4.64
2006-01-01 01:45:00 0 4.64
2006-01-01 02:00:00 725.24 4.64
I want to delete all rows where 0 values appear, to receive:
quir schme
2006-01-01 01:15:00 725.24 4.64
2006-01-01 01:30:00 725.24 4.64
2006-01-01 02:00:00 725.24 4.64
I didn't find a proper way to to this for zoo-objects so I used the very unelegant way to export the data to a *.csv and treat the rows upon re-import. Anyone who is willing to share a more elegant way?
Best regards Jochen
Upvotes: 1
Views: 1528
Reputation: 887048
You can use subset
or [
library(zoo)
subset(z1, quir!=0)
# quir schme
#2006-01-01 01:15:00 725.24 4.64
#2006-01-01 01:30:00 725.24 4.64
#2006-01-01 02:00:00 725.24 4.64
If you need to remove rows if there are 0
values in any of the columns
z1[!rowSums(z1==0),]
# quir schme
#2006-01-01 01:15:00 725.24 4.64
#2006-01-01 01:30:00 725.24 4.64
#2006-01-01 02:00:00 725.24 4.64
Or as @G.Grothendieck mentioned in the comments, we can loop over the rows with apply
and get the logical index with all
or any
. Here, !
indicates negation. !!
double negation (z1!=0
and !!z1
are the same).
z1[apply(z1 != 0, 1, all), ]
z1[apply(!!z1, 1, all), ]
z1[!apply(z1 == 0, 1, any), ]
z1 <- structure(c(725.24, 725.24, 0, 725.24, 4.64, 4.64, 4.64, 4.64
), .Dim = c(4L, 2L), .Dimnames = list(NULL, c("quir", "schme"
)), index = structure(c(1136096100, 1136097000, 1136097900, 1136098800
), class = c("POSIXct", "POSIXt"), tzone = ""), class = "zoo")
Upvotes: 2