Hi R users, Is there a function in R, which does some calculation only for the month end in a daily data?... In other words, is there a command in R, equivalent to "last." function in SAS? BR, Shubha [[alternative HTML version deleted]]
Hi, Perhaps if object is of the type 'ts', the command 'end' can usefully. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O On 29/08/2007, Shubha Vishwanath Karanth <shubhak@ambaresearch.com> wrote:> > Hi R users, > > > > Is there a function in R, which does some calculation only for the month > end in a daily data?... In other words, is there a command in R, > equivalent to "last." function in SAS? > > > > BR, Shubha > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
A simple example (avoiding using dates, just to show the principle) - this assumes that your data are already sorted (?order). temp <- data.frame(subject = rep(1:2, each = 5), response = 1:10) print(temp) last <- do.call(rbind, by(temp, temp$subject, function(x) tail(x, 1))) print(last) By changing the 2nd parameter to 'tail' you can get different numbers of observations in the tail of each subset, so it has more power than SAS's .last. If you need an equivalent of .first, then replace 'tail' with 'head'. Jim. Shubha Vishwanath Karanth wrote:> > Hi R users, > > > > Is there a function in R, which does some calculation only for the month > end in a daily data?... In other words, is there a command in R, > equivalent to "last." function in SAS? > > > > BR, Shubha > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/Month-end-calculations-tf4347226.html#a12390874 Sent from the R help mailing list archive at Nabble.com.
Hi Shubha, By using the tautology that the end of a month is immediately followed by the first of a month, the following returns a TRUE when the date is the last day of a month IsMonthEnd <- format(MyDates + 1, "%d") == "01" where MyDates is a vector, or column in a data frame, typed as Date (eg with as.Date) -- HTH, Jim Porzak Responsys, Inc. San Francisco, CA http://www.linkedin.com/in/jimporzak On 8/29/07, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> Hi R users, > > > > Is there a function in R, which does some calculation only for the month > end in a daily data?... In other words, is there a command in R, > equivalent to "last." function in SAS? > > > > BR, Shubha > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Shubha, I apologize if this is a bit late - consequence of digest summary preference. If I understand what you need, it is to calculate the value at month end given a data object. For zoo objects the following should do what you need. Actually is part of my new package on CRAN quantmod - basically a workflow management tool for quant finance modelling. Also visible at www.quantmod.com If you convert your data.frame to a zoo object (designed for ordered obs. - e.g. time-series) # for your data - which I don't know : ) zoo.ts <- zoo(youdataframe[,-1],as.Date(yourdataframe[,1])) # ^ ^ # ^ ^ ^ ^ # minus 'date' column the 'date' column - in CCYY-MM-DD format My example: A zoo time series object consisting of 231 days:> zoo.ts <- zoo(rnorm(231),as.Date(13514:13744)) > start(zoo.ts)[1] "2007-01-01"> end(zoo.ts)[1] "2007-08-19" # these are the end points of each period> breakpoints(zoo.ts,months,TRUE)[1] 0 31 59 90 120 151 181 212 231 # get the associated values> zoo.ts[breakpoints(zoo.ts,months,TRUE)]2007-01-31 2007-02-28 2007-03-31 2007-04-30 2007-05-31 2007-06-30 -0.008829668 -2.207802921 0.171705151 -1.820125167 1.776643162 0.884558259 2007-07-31 2007-08-19 0.655305543 0.191870144 You can also apply a function inside each of these periods (intervals) with the function period.apply: e.g. the standard deviation of each period would be had with:> period.apply(zoo.ts,breakpoints(zoo.ts,months,TRUE),FUN=sd)[1] 0.9165168 1.2483743 1.0717529 1.2002236 0.9568443 0.8112068 0.8563814 [8] 0.8671502 The functions (and many others) are in quantmod - on CRAN and most up to date at www.quantmod.com For those who'd rather just have the functions: breakpoints <- function (x, by = c(weekdays, weeks, months, quarters, years), ...) { if (length(by) != 1) stop("choose ONE method for \"by\"") by <- match.fun(by) breaks <- which(diff(as.numeric(by(x, ...))) != 0) breaks <- c(0, breaks, NROW(x)) return(breaks) } period.apply <- function (x, INDEX, FUN, ...) { FUN <- match.fun(FUN) y <- NULL for (i in 1:(length(INDEX) - 1)) { sindex <- (INDEX[i] + 1):INDEX[i + 1] dat <- x[sindex] y <- c(y, FUN(dat, ...)) } return(y) } Jeff Ryan
The zoo package includes the "yearmon" class to facilitate such manipulations. Here are a few solutions assuming you store you series in a zoo variable: # test data library(zoo) z <- zoo(1001:1100, as.Date(101:200))[-(45:55)] # Solution 1. tapply produces indexes of last of month tt <- time(z) z[ c(tapply(seq_along(tt), as.yearmon(tt), tail, 1)) ] # If we want to create a last variable which corresponds # to last in sas then do it this slightly longer way: # Solution 2 tt <- time(z) last <- seq_along(tt) %in% tapply(seq_along(tt), as.yearmon(tt), tail, 1) z[last] # Solution 3. another solution with a last variable. f(x) is # vector same length as x with all 0's except last element is 1. tt <- time(z) f <- function(x) replace(0*x, length(x), 1) last <- ave(seq_along(tt), as.yearmon(tt), FUN = f) z[last] In all these solutions the last point in the series is always included. We have not assumed that every day is necessarily included in your series but if every day is included then even simpler solutions are possible. On 8/29/07, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> Hi R users, > > > > Is there a function in R, which does some calculation only for the month > end in a daily data?... In other words, is there a command in R, > equivalent to "last." function in SAS? > > > > BR, Shubha > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >