Questions tagged [zoo]

zoo is an R package that provides an S3 class with methods for totally ordered indexed observations.

zoo ("Z's Ordered Observations") is an package for analysis of regular and irregular time series.

It provides an class with methods for totally ordered indexed observations. It is particularly aimed at irregular of numeric vectors/matrices and factors. zoo's key design goals are independence of a particular index/date/time class and consistency with ts in base-R by providing methods to extend standard generics.

Repositories

Vignettes

Other resources

Related tags

1225 questions
161
votes
18 answers

Replacing NAs with latest non-NA value

In a data.frame (or data.table), I would like to "fill forward" NAs with the closest previous non-NA value. A simple example, using vectors (instead of a data.frame) is the following: > y <- c(NA, 2, 2, NA, NA, 3, NA, 4, NA, NA) I would like a…
Ryogi
  • 4,907
  • 5
  • 22
  • 43
115
votes
7 answers

Extract month and year from a zoo::yearmon object

I have a yearmon object: require(zoo) date1 <- as.yearmon("Mar 2012", "%b %Y") class(date1) # [1] "yearmon" How can I extract the month and year from this? month1 <- fn(date1) year1 <- fn(date1) What function should I use in place of fn()
adam.888
  • 6,958
  • 16
  • 58
  • 97
97
votes
7 answers

Converting year and month ("yyyy-mm" format) to a date?

I have a dataset that looks like this: Month count 2009-01 12 2009-02 310 2009-03 2379 2009-04 234 2009-05 14 2009-08 1 2009-09 34 2009-10 2386 I want to plot the data (months as x values and counts as y values). Since there are gaps in…
R_User
  • 9,332
  • 22
  • 68
  • 115
54
votes
5 answers

R not finding package even after package installation

I have always worked with the zoo package, that I have installed a long time ago. Today, I created a new R script, and ran library(zoo) and got the following error: > library(zoo) Error in library(zoo) : there is no package called ‘zoo’ Odd..…
Mayou
  • 7,628
  • 15
  • 51
  • 91
53
votes
2 answers

How to exclude one column from data.table OR convert to data.table to MTS

When using data.table is it possible to return all the columns except one, like in data.frame? If the answer is no, does anyone have an elegant way to transform a multiple time series data.table to a zoo or other time series object? Consider the…
geneorama
  • 3,057
  • 4
  • 24
  • 33
39
votes
13 answers

Basic lag in R vector/dataframe

Will most likely expose that I am new to R, but in SPSS, running lags is very easy. Obviously this is user error, but what I am missing? x <- sample(c(1:9), 10, replace = T) y <- lag(x, 1) ds <- cbind(x, y) ds Results in: x y [1,] 4 4 [2,]…
Btibert3
  • 34,187
  • 40
  • 119
  • 164
38
votes
5 answers

Replace NA with previous or next value, by group, using dplyr

I have a data frame which is arranged by descending order of date. ps1 = data.frame(userID = c(21,21,21,22,22,22,23,23,23), color = c(NA,'blue','red','blue',NA,NA,'red',NA,'gold'), age =…
Tarak
  • 775
  • 2
  • 6
  • 12
26
votes
5 answers

optimized rolling functions on irregular time series with time-based window

Is there some way to use rollapply (from zoo package or something similar) optimized functions (rollmean, rollmedian etc) to compute rolling functions with a time-based window, instead of one based on a number of observations? What I want is simple:…
vsalmendra
  • 547
  • 1
  • 6
  • 13
24
votes
2 answers

Access zoo or xts index

I am using zoo objects, buy my question also applies to xts objects. It looks to me like it is a one column vector with an index. In my case the index is the vector of dates and the one column vector my data. All is good except that I would like to…
feschet
  • 241
  • 1
  • 2
  • 3
18
votes
2 answers

R: Filling missing dates in a time series?

I have a zoo time series with missing days. In order to fill it and have a continuous series I do... I generate a chron date-time sequence from start to end. I merge my series with this one. I use na.locf to substitute NAs with las obsservation. I…
skan
  • 6,456
  • 11
  • 47
  • 83
17
votes
4 answers

R: merge two irregular time series

I have two multivariate time series x and y, both covering approximately the same range in time (one starts two years before the other, but they end on the same date). Both series have missing observations in the form of empty columns next to the…
Benjamin Allévius
  • 797
  • 1
  • 5
  • 14
17
votes
3 answers

Adaptive moving average - top performance in R

I am looking for some performance gains in terms of rolling/sliding window functions in R. It is quite common task which can be used in any ordered observations data set. I would like to share some of my findings, maybe somebody would be able to…
jangorecki
  • 14,077
  • 3
  • 57
  • 137
17
votes
2 answers

Filling in missing (blanks) in a data table, per category - backwards and forwards

I am working with a large data set of billing records for my clinical practice over 11 years. Quite a few of the rows are missing the referring physician. However, using some rules I can quite easily fill them in but do not know how to implement it…
Farrel
  • 9,584
  • 19
  • 57
  • 95
16
votes
3 answers

Reading csv with date and time

I am working in R and reading csv which has date and time in its first column. I want to import this csv file in R first and then convert it to zoo obect. I am using the code in R EURUSD <- as.xts(read.zoo("myfile.csv",sep=",",tz="",header=T)) My…
user395882
  • 547
  • 2
  • 6
  • 15
16
votes
1 answer

Aggregate by week in R

In R I frequently aggregate daily data (in a zoo) by month, using something like this: result <- aggregate(x, as.yearmon, "mean", na.rm=TRUE) Is there a way that I can do this by week?
Richard Herron
  • 8,979
  • 10
  • 62
  • 104
1
2 3
81 82