[R] Windows Installation Without Third-Party Packages
I am trying to install R for Windows, but when I use the installer provided on CRAN, a number of third-party packages are installed by default (i.e. lattice, Matrix, codetools, etc.). If R is installed with administrator privileges, so it's available for all users, non-administrators can't update those packages. Is there any way to just install R without any third-party packages, and let individual users install the packages they want? Thanks. - Elliot [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Repeated Aggregation with data.table
I still haven't come up with a solution to the question below, and I have another one. I frequently find myself in a situation where I have the list of columns I want to aggregate over in the form of a vector of strings, and I have to do something like the following: dat[, list(mean.z = mean(z)), by = eval(parse(text = sprintf("list(%s)", paste(x, collapse=","] I think that's a pretty ugly solution (although it does work), but I haven't come up with anything better. Any suggestions? Thanks. - Elliot On Tue, Sep 11, 2012 at 11:33 AM, Elliot Joel Bernstein < elliot.bernst...@fdopartners.com> wrote: > I've been using this setup: > > > > flist <- expression( list(mean.z = mean(z), sd.z = sd(z)) ) > > dat[ , eval(flist), list(x)] > > It works great, but there's one small catch. If I do something like > > > flist <- expression(list(x.per.y = sum(x) / sum(y))) > > dat[, eval(flist), list(y)] > > it does the wrong thing, because sum(y) in each group is just the common > value, rather than that value times the length. Is there any way around > this? Obviously I could rewrite the expression if I know I'm going to by > grouping by y, but I'd like it to be generic. > > Thanks. > > - Elliot > > > On Wed, Aug 8, 2012 at 9:17 AM, David Winsemius wrote: > >> >> On Aug 7, 2012, at 9:28 PM, arun wrote: >> >> HI, >>> >>> Try this: >>> >>> fun1<-function(x,.expr){ >>> .expr<-expression(list(mean.z=**mean(z),sd.z=sd(z))) >>> z1<-eval(.expr) >>> } >>> >>> #or >>> fun1<-function(x,.expr){ >>> .expr<-expression(list(mean.z=**mean(z),sd.z=sd(z))) >>> z1<-.expr >>> } >>> >>> >>> dat[,eval(z1),list(x)] >>> dat[,eval(z1),list(y)] >>> dat[,eval(z1),list(x,y)] >>> >>> >> I'm not seeing the connection between those functions and the data.table >> call. (Running that code produces an error on my machine.) If the goal is >> to have an expression result then just create it with expression(). In the >> example: >> >> > flist <- expression( list(mean.z = mean(z), sd.z = sd(z)) ) >> > dat[ , eval(flist), list(x)] >>x mean.z sd.z >> 1: 2 0.04436034 1.039615 >> 2: 3 -0.06354504 1.077686 >> 3: 1 -0.08879671 1.066916 >> >> -- >> David. >> >> >> A.K. >>> >>> >>> >>> - Original Message - >>> From: Elliot Joel Bernstein >>> >>> > >>> To: r-help@r-project.org >>> Cc: >>> Sent: Tuesday, August 7, 2012 5:36 PM >>> Subject: [R] Repeated Aggregation with data.table >>> >>> I have been using ddply to do aggregation, and I frequently define a >>> single aggregation function that I use to aggregate over different >>> groups. For example, >>> >>> require(plyr) >>> >>> dat <- data.frame(x = sample(3, 100, replace=TRUE), y = sample(3, 100, >>> replace = TRUE), z = rnorm(100)) >>> >>> f <- function(x) { data.frame(mean.z = mean(x$z), sd.z = sd(x$z)) } >>> >>> ddply(dat, "x", f) >>> ddply(dat, "y", f) >>> ddply(dat, c("x", "y"), f) >>> >>> I recently discovered the data.table package, which dramatically >>> speeds up the aggregation: >>> >>> require(data.table) >>> dat <- data.table(dat) >>> >>> dat[, list(mean.z = mean(z), sd.z = sd(z)), list(x)] >>> dat[, list(mean.z = mean(z), sd.z = sd(z)), list(y)] >>> dat[, list(mean.z = mean(z), sd.z = sd(z)), list(x,y)] >>> >>> But I can't figure out how to save the aggregation function >>> "list(mean.z = mean(z), sd.z = sd(z))" as a variable that I can reuse, >>> similar to the function "f" above. Can someone please explain how to >>> do that? >>> >>> Thanks. >>> >>> - Elliot >>> >>> -- >>> Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC >>> 134 Mount Auburn Street | Cambridge, MA | 02138 >>> Phone: (617) 503-4619 | Email: >>> elliot.bernstein@fdopartners.**com >>> >>> ______**____ >>> R-help@r-project.org mailing list >>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >>> PLEASE do read th
Re: [R] Repeated Aggregation with data.table
I've been using this setup: > flist <- expression( list(mean.z = mean(z), sd.z = sd(z)) ) > dat[ , eval(flist), list(x)] It works great, but there's one small catch. If I do something like > flist <- expression(list(x.per.y = sum(x) / sum(y))) > dat[, eval(flist), list(y)] it does the wrong thing, because sum(y) in each group is just the common value, rather than that value times the length. Is there any way around this? Obviously I could rewrite the expression if I know I'm going to by grouping by y, but I'd like it to be generic. Thanks. - Elliot On Wed, Aug 8, 2012 at 9:17 AM, David Winsemius wrote: > > On Aug 7, 2012, at 9:28 PM, arun wrote: > > HI, >> >> Try this: >> >> fun1<-function(x,.expr){ >> .expr<-expression(list(mean.z=**mean(z),sd.z=sd(z))) >> z1<-eval(.expr) >> } >> >> #or >> fun1<-function(x,.expr){ >> .expr<-expression(list(mean.z=**mean(z),sd.z=sd(z))) >> z1<-.expr >> } >> >> >> dat[,eval(z1),list(x)] >> dat[,eval(z1),list(y)] >> dat[,eval(z1),list(x,y)] >> >> > I'm not seeing the connection between those functions and the data.table > call. (Running that code produces an error on my machine.) If the goal is > to have an expression result then just create it with expression(). In the > example: > > > flist <- expression( list(mean.z = mean(z), sd.z = sd(z)) ) > > dat[ , eval(flist), list(x)] >x mean.z sd.z > 1: 2 0.04436034 1.039615 > 2: 3 -0.06354504 1.077686 > 3: 1 -0.08879671 1.066916 > > -- > David. > > > A.K. >> >> >> >> - Original Message - >> From: Elliot Joel Bernstein >> >> > >> To: r-help@r-project.org >> Cc: >> Sent: Tuesday, August 7, 2012 5:36 PM >> Subject: [R] Repeated Aggregation with data.table >> >> I have been using ddply to do aggregation, and I frequently define a >> single aggregation function that I use to aggregate over different >> groups. For example, >> >> require(plyr) >> >> dat <- data.frame(x = sample(3, 100, replace=TRUE), y = sample(3, 100, >> replace = TRUE), z = rnorm(100)) >> >> f <- function(x) { data.frame(mean.z = mean(x$z), sd.z = sd(x$z)) } >> >> ddply(dat, "x", f) >> ddply(dat, "y", f) >> ddply(dat, c("x", "y"), f) >> >> I recently discovered the data.table package, which dramatically >> speeds up the aggregation: >> >> require(data.table) >> dat <- data.table(dat) >> >> dat[, list(mean.z = mean(z), sd.z = sd(z)), list(x)] >> dat[, list(mean.z = mean(z), sd.z = sd(z)), list(y)] >> dat[, list(mean.z = mean(z), sd.z = sd(z)), list(x,y)] >> >> But I can't figure out how to save the aggregation function >> "list(mean.z = mean(z), sd.z = sd(z))" as a variable that I can reuse, >> similar to the function "f" above. Can someone please explain how to >> do that? >> >> Thanks. >> >> - Elliot >> >> -- >> Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC >> 134 Mount Auburn Street | Cambridge, MA | 02138 >> Phone: (617) 503-4619 | Email: >> elliot.bernstein@fdopartners.**com >> >> __** >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >> PLEASE do read the posting guide http://www.R-project.org/** >> posting-guide.html <http://www.R-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> >> >> __** >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >> PLEASE do read the posting guide http://www.R-project.org/** >> posting-guide.html <http://www.R-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> > > David Winsemius, MD > Alameda, CA, USA > > -- Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC 134 Mount Auburn Street | Cambridge, MA | 02138 Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com [[alternative HTML version deleted]] __ R-help@r-project.org 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.
[R] Repeated Aggregation with data.table
I have been using ddply to do aggregation, and I frequently define a single aggregation function that I use to aggregate over different groups. For example, require(plyr) dat <- data.frame(x = sample(3, 100, replace=TRUE), y = sample(3, 100, replace = TRUE), z = rnorm(100)) f <- function(x) { data.frame(mean.z = mean(x$z), sd.z = sd(x$z)) } ddply(dat, "x", f) ddply(dat, "y", f) ddply(dat, c("x", "y"), f) I recently discovered the data.table package, which dramatically speeds up the aggregation: require(data.table) dat <- data.table(dat) dat[, list(mean.z = mean(z), sd.z = sd(z)), list(x)] dat[, list(mean.z = mean(z), sd.z = sd(z)), list(y)] dat[, list(mean.z = mean(z), sd.z = sd(z)), list(x,y)] But I can't figure out how to save the aggregation function "list(mean.z = mean(z), sd.z = sd(z))" as a variable that I can reuse, similar to the function "f" above. Can someone please explain how to do that? Thanks. - Elliot -- Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC 134 Mount Auburn Street | Cambridge, MA | 02138 Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com __ R-help@r-project.org 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.
Re: [R] Thinning Lattice Plot
I see. I typically use a (one-sided) formula as the first argument to Ecdf, but didn't even think about that distinction in putting together this example. Thanks again for your help. - Elliot On Tue, Jul 31, 2012 at 12:46 PM, Deepayan Sarkar wrote: > On Tue, Jul 31, 2012 at 6:43 PM, Elliot Joel Bernstein > wrote: > > > Thanks everyone for your replies. I didn't know about the ecdfplot > function, > > so I'll start using that instead of Ecdf. Why is Ecdf not a lattice plot? > > The result certainly looks like other lattice plots, the arguments are > > similar to other lattice plots. In fact, internally it seems to just call > > the "histogram" function with a different prepanel and panel function. > Is it > > not considered a lattice plot only because it isn't part of the lattice > > package? > > Of course not. What you are saying is a valid description of the > Ecdf.formula() method, which definitely produces a lattice plot (or > trellis plot if you prefer). However, the example you gave, namely, > > x <- rnorm(1e6) > Ecdf(x) > > ends up calling Ecdf.default(), which is very much a traditional > graphics function. I should add that this is for Hmisc 3.9-2, and > don't know if the behaviour is different with other versions. > > Note that Ecdf() has more features than ecdfplot(), in particular it > allows weights. > > -Deepayan > -- Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC 134 Mount Auburn Street | Cambridge, MA | 02138 Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com [[alternative HTML version deleted]] __ R-help@r-project.org 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.
Re: [R] Error Installing Package with Dependency on "Matrix"
A follow up on this issue...if I first build the package using "R CMD build temp", I can install the resulting .tar.gz file from within R using the "install.packages" command, but I still can't install it from the command line using "R CMD INSTALL". - Elliot On Tue, Jul 31, 2012 at 11:33 AM, Elliot Joel Bernstein < elliot.bernst...@fdopartners.com> wrote: > I'm attempting to update to R 2.15.1, and I'm having trouble with a > package that depends on the "Matrix" package. I've created a dummy package > consisting only of a DESCRIPTION file that specifies the dependence on > "Matrix", a NAMESPACE file, and an R directory, containing a single > function, "square <- function(x) { return (x*x) }". When I try to install, > I get the following error: > > [ebernstein@coolio R]$ R CMD INSTALL temp -l /home/ebernstein/Rlib_2.15.1/ > * installing *source* package temp ... > ** R > ** preparing package for lazy loading > Error : .onLoad failed in loadNamespace() for 'Matrix', details: > call: assignInNamespace("..Old..as.matrix", base::as.matrix, ns = "base") > error: locked binding of ..Old..as.matrix cannot be changed > Error : package Matrix could not be loaded > ERROR: lazy loading failed for package temp > * removing /home/ebernstein/Rlib_2.15.1/temp > > If I remove the dependency on "Matrix", it installs fine. I'm using R > 2.15.1 on linux, and version 1.0-6 of the Matrix package. Can anyone > explain what's going on here? > > Thanks. > > - Elliot > > -- > Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC > 134 Mount Auburn Street | Cambridge, MA | 02138 > Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com > > -- Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC 134 Mount Auburn Street | Cambridge, MA | 02138 Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com [[alternative HTML version deleted]] __ R-help@r-project.org 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.
[R] Error Installing Package with Dependency on "Matrix"
I'm attempting to update to R 2.15.1, and I'm having trouble with a package that depends on the "Matrix" package. I've created a dummy package consisting only of a DESCRIPTION file that specifies the dependence on "Matrix", a NAMESPACE file, and an R directory, containing a single function, "square <- function(x) { return (x*x) }". When I try to install, I get the following error: [ebernstein@coolio R]$ R CMD INSTALL temp -l /home/ebernstein/Rlib_2.15.1/ * installing *source* package temp ... ** R ** preparing package for lazy loading Error : .onLoad failed in loadNamespace() for 'Matrix', details: call: assignInNamespace("..Old..as.matrix", base::as.matrix, ns = "base") error: locked binding of ..Old..as.matrix cannot be changed Error : package Matrix could not be loaded ERROR: lazy loading failed for package temp * removing /home/ebernstein/Rlib_2.15.1/temp If I remove the dependency on "Matrix", it installs fine. I'm using R 2.15.1 on linux, and version 1.0-6 of the Matrix package. Can anyone explain what's going on here? Thanks. - Elliot -- Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC 134 Mount Auburn Street | Cambridge, MA | 02138 Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com [[alternative HTML version deleted]] __ R-help@r-project.org 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.
Re: [R] Thinning Lattice Plot
Thanks everyone for your replies. I didn't know about the ecdfplot function, so I'll start using that instead of Ecdf. Why is Ecdf not a lattice plot? The result certainly looks like other lattice plots, the arguments are similar to other lattice plots. In fact, internally it seems to just call the "histogram" function with a different prepanel and panel function. Is it not considered a lattice plot only because it isn't part of the lattice package? Thanks. - Elliot On Tue, Jul 31, 2012 at 2:32 AM, Deepayan Sarkar wrote: > On Tue, Jul 31, 2012 at 2:43 AM, Elliot Joel Bernstein > wrote: > > Is there an easy way to "thin" a lattice plot? I often create plots from > > large data sets, and use the "pdf" command to save them to a file, but > the > > resulting files can be huge, because every point in the underlying > dataset > > is rendered in the plot, even though it isn't possible to see that much > > detail. > > > > For example: > > > > require(Hmisc) > > x <- rnorm(1e6) > > > > pdf("test.pdf") > > Ecdf(x) > > dev.off() > > (This is not a lattice plot, BTW.) > > > The resulting pdf files is 31MB. > > Hmm, for me it's 192K. Perhaps you have not bothered to update R recently. > > > Is there any easy way to get a smaller pdf > > file without having to manually prune the dataset? > > In general, as David noted, you need to do some sort of data > summarization; great if tools are available to that, otherwise > yourself. In this case, for example, it seems reasonable to do > > Ecdf(quantile(x, probs = ppoints(500, a=1))) > > If you don't like to do this yourself, ecdfplot() in latticeExtra will > allow > > library(latticeExtra) > ecdfplot(x, f.value = ppoints(500, a=1)) > > -Deepayan > -- Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC 134 Mount Auburn Street | Cambridge, MA | 02138 Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com [[alternative HTML version deleted]] __ R-help@r-project.org 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.
[R] Thinning Lattice Plot
Is there an easy way to "thin" a lattice plot? I often create plots from large data sets, and use the "pdf" command to save them to a file, but the resulting files can be huge, because every point in the underlying dataset is rendered in the plot, even though it isn't possible to see that much detail. For example: require(Hmisc) x <- rnorm(1e6) pdf("test.pdf") Ecdf(x) dev.off() The resulting pdf files is 31MB. Is there any easy way to get a smaller pdf file without having to manually prune the dataset? Thanks. - Elliot -- Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC 134 Mount Auburn Street | Cambridge, MA | 02138 Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com [[alternative HTML version deleted]] __ R-help@r-project.org 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.
[R] combineLimits and Dates
I'm having some trouble using the latticeExtra 'combineLimits' function with a Date x-variable: require(lattice) set.seed(12345) dates <- seq(as.Date("2011-01-01"), as.Date("2011-12-31"), "days") dat <- data.frame(d = rep(dates, 4), g = factor(rep(rep(c(1,2), each = length(dates)), 2)), h = factor(rep(c("a", "b"), each = length(dates)*2)), x = rnorm(4 * length(dates)), y = rnorm(4 * length(dates))) plt1 <- xyplot(x + y ~ d | g, groups = h, data = dat, type = 'l', scales = list(relation = "free"), auto.key = TRUE) plt1 <- useOuterStrips(plt1) plt1 <- combineLimits(plt1) The x-axis labels are right after the call to 'useOuterStrips' but they get converted to numeric after the call to 'combineLimits'. How do I keep them as date labels? Thanks. - Elliot -- Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC 134 Mount Auburn Street | Cambridge, MA | 02138 Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com [[alternative HTML version deleted]] __ R-help@r-project.org 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.
[R] "Tight" Axes in Prepanel Function
How do I specify a "tight" y-axis, where the plot completely fills the y-axis range, inside the prepanel function? For example, consider the following code: require(lattice) set.seed(12345) x <- 1:1000 y <- cumsum(rnorm(length(x))) prepanel.test <- function(x, y, groups = NULL, subscripts = NULL, ...) { if (is.null(groups)) { result <- list(ylim = range(y)) } else { result <- list(ylim = range(y, finite = TRUE)) } return (result) } print(xyplot(y~x, prepanel = prepanel.test, type = 'l', grid = TRUE), split = c(1,1,1,2), more = TRUE) print(xyplot(y~x, ylim = range(y, finite = TRUE), type = 'l', grid = TRUE), split = c(1,2,1,2)) The top plot has extra whitespace at the top and bottom. Is there any way to eliminate this without having to specify 'ylim' directly in the call the 'xyplot'? (The reason I want to include this in the prepanel function is that I want to add conditioning and use the "combineLimits" function in the latticeExtra package.) Thanks. - Elliot [[alternative HTML version deleted]] __ R-help@r-project.org 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.
[R] splom Tick Location
When using the 'splom' function of the 'lattice' packge, is it possible to get all the tick marks in the outer margins of the plot? X <- as.data.frame(matrix(rnorm(1000), 100, 10)) plot(X) ## Tick marks are in the outer margin splom(X) ## Tick marks are inside the on-diagonal panels Thanks. - Elliot [[alternative HTML version deleted]] __ R-help@r-project.org 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.
[R] Errors and line numbers in scripts?
Is it possible to get R to report the line number of an error when a script is called with source()? I found the following post from 2009, but it's not clear to me if this ever made it into the release version: ws wrote: >* Is there a way to have R return the line number in a script when it errors >out? *>* *>* I call my script like: *>* *>* $ R --vanilla < script.R > output.txt *>* *>* I seem to remember a long discussion about this at some point, but I can't *>* remember the outcome. *>* *>* * The current development version returns much more information about error locations. I don't know if it will handle this case (R doesn't get told the filename in case of input redirection, for example). Generally the goal is to report error lines during interactive use, batch use is assumed to already be debugged. Duncan Murdoch Thanks. - Elliot [[alternative HTML version deleted]] __ R-help@r-project.org 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.
Re: [R] Cumsum in Lattice Panel Function
That worked perfectly. Thanks! - Elliot On Mon, May 09, 2011 at 12:20:36AM +0530, Deepayan Sarkar wrote: > On Fri, May 6, 2011 at 9:24 PM, Elliot Joel Bernstein > wrote: > > I'm trying to create an xyplot with a "groups" argument where the y-variable > > is the cumsum of the values stored in the input data frame. I almost have > > it, but I can't get it to automatically adjust the y-axis scale. How do I > > get the y-axis to automatically scale as it would have if the cumsum values > > had been stored in the data frame? > > > > Here is the code I have so far: > > > > require(lattice) > > > > > > > > dates <- seq(as.Date("2011-01-01"), as.Date("2011-04-30"), "days") > > g <- 1:3 > > > > > > dat <- data.frame(date = rep(dates, length(g)), > > group = rep(g, each = length(dates)), > > value = rnorm(length(dates)*length(g)) + 0.05) > > > > > > xyplot(value ~ date, data = dat, group = group, type = 'l', grid = TRUE, > > panel = panel.superpose, > > panel.groups = function(x, y, ...) { panel.xyplot(x, cumsum(y), ...) > > }) > > > > > > I want the result to look the same as if I had done > > > > dat$cumvalue <- with(dat, unsplit(lapply(split(value, group), cumsum), > > group)) > > xyplot(cumvalue ~ date, data = dat, group = group, type = 'l', grid = TRUE) > > You need something along the lines of > > xyplot(value ~ date, data = dat, group = group, type = 'l', grid = TRUE, >panel = panel.superpose, > panel.groups = function(x, y, ...) { >panel.xyplot(x, cumsum(y), ...) >}, >prepanel = function(x, y, groups, ...) { >yy <- unlist(tapply(y, groups, cumsum)) >list(ylim = range(yy, finite = TRUE)) >}) > > -Deepayan -- Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC 134 Mount Auburn Street | Cambridge, MA | 02138 Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com __ R-help@r-project.org 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.
[R] Cumsum in Lattice Panel Function
I'm trying to create an xyplot with a "groups" argument where the y-variable is the cumsum of the values stored in the input data frame. I almost have it, but I can't get it to automatically adjust the y-axis scale. How do I get the y-axis to automatically scale as it would have if the cumsum values had been stored in the data frame? Here is the code I have so far: require(lattice) dates <- seq(as.Date("2011-01-01"), as.Date("2011-04-30"), "days") g <- 1:3 dat <- data.frame(date = rep(dates, length(g)), group = rep(g, each = length(dates)), value = rnorm(length(dates)*length(g)) + 0.05) xyplot(value ~ date, data = dat, group = group, type = 'l', grid = TRUE, panel = panel.superpose, panel.groups = function(x, y, ...) { panel.xyplot(x, cumsum(y), ...) }) I want the result to look the same as if I had done dat$cumvalue <- with(dat, unsplit(lapply(split(value, group), cumsum), group)) xyplot(cumvalue ~ date, data = dat, group = group, type = 'l', grid = TRUE) Thanks for your help. - Elliot Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC 134 Mount Auburn Street | Cambridge, MA | 02138 Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com [[alternative HTML version deleted]] __ R-help@r-project.org 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.
Re: [R] Scaling Lattice Graphics for tikzDevice
On Feb 18, 2011 11:02 PM, "Deepayan Sarkar" wrote: > > On Fri, Feb 18, 2011 at 11:04 PM, Elliot Joel Bernstein > wrote: > > I'm trying to use lattice graphics to produce some small plots for inclusion in a LaTeX file. I want the LaTeX fonts to be used in the plots, but to be scaled down to match the size of the plot. I have written the following code to apply a scaling factor to all the "cex" and "padding" entries in the trellis parameters, but there is still a large white space between the key and the top of the plot area. Does anyone know how I can get rid of that (or if I'm going about this all wrong and there's a much cleaner way)? > > > > ## -- ## > > ## test.R ## > > ## -- ## > > > > require(tikzDevice) > > I have not (yet) used tikzDevice, so don't know what impact it will > have, but in my experience with pdf(), it's usually more effective to > create a larger file and then scale it when including it in the .tex > file. > > -Deepayan > > > require(lattice) > > > > rescale.pars <- function(cex.factor = 0.5, > > padding.factor = 0.25, > > pars = trellis.par.get()) { > > > > result <- NULL > > for (i in seq_along(pars)) { > > > >if (names(pars)[[i]] == "cex") { > > > > if (is.null(result)) { > >result <- list() > > } > > result <- c(result, cex=cex.factor*pars[[i]]) > > > >} else if (grepl("padding$", names(pars)[[i]])) { > > > > if (is.null(result)) { > >result <- list() > > } > > eval(parse(text=sprintf("result <- c(result, %s=padding.factor*pars[[i]])", names(pars)[[i]]))) > > > >} else if (inherits(pars[[i]], "list")) { > > > > temp <- rescale.pars(cex.factor, padding.factor, pars[[i]]) > > if (!is.null(temp)) { > >result[[names(pars)[[i <- temp > > } > > > >} > > } > > > > return (result) > > } > > > > x <- 1:100 > > y <- cumsum(rnorm(length(x), 1, 10)) > > z <- cumsum(rnorm(length(x), 1, 20)) > > > > tikz("test-plot.tex", width=2, height=2) > > > > xyplot(y + z ~ x, > > main = "My Plot", > > xlab = "x", > > auto.key=list(text=c("Line 1", "Line 2"), points=FALSE, lines=TRUE), > > grid = TRUE, > > par.settings = rescale.pars()) > > > > dev.off() > > > > ## -- ## > > ## END R CODE ## > > ## -- ## > > > > Thanks. > > > > - Elliot > > > > - > > Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC > > 134 Mount Auburn Street | Cambridge, MA | 02138 > > Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com > > > > __ > > R-help@r-project.org 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. > > Deepayan - Thank you for your reply. In the past I've generally done what you suggest, but I'm trying to use tikz so the graphics can inherit the fonts used in the LaTeX document. Is there a way to do that with the pdf device? Thanks. - Elliot [[alternative HTML version deleted]] __ R-help@r-project.org 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.
[R] Scaling Lattice Graphics for tikzDevice
} else if (inherits(pars[[i]], "list")) { temp <- rescale.pars(cex.factor, padding.factor, pars[[i]]) if (!is.null(temp)) { result[[names(pars)[[i <- temp } } } return (result) } x <- 1:100 y <- cumsum(rnorm(length(x), 1, 10)) z <- cumsum(rnorm(length(x), 1, 20)) tikz("test-plot.tex", width=2, height=2) xyplot(y + z ~ x, main = "My Plot", xlab = "x", auto.key=list(text=c("Line 1", "Line 2"), points=FALSE, lines=TRUE), grid = TRUE, par.settings = rescale.pars()) dev.off() ## -- ## ## END R CODE ## ## -- ## Thanks. - Elliot ---
[R] Generic Functions and Dates
I'm trying to write a generic function that calls different methods depending on the structure of the argument, but not the exact type of its contents. For example, the function 'nan2last' below works for a numeric vector but not for a vector of Date objects. Is there any way to make it work on any vector? setGeneric("nan2last", function(x) { standardGeneric("nan2last") }) setMethod(nan2last, "vector", function(x) { naLocs <- (1:length(x))[is.na(x)] if (length(naLocs) == 0) return (x) naLocs <- naLocs[naLocs>1] for (i in 1:length(naLocs)) { x[naLocs[i]] <- x[naLocs[i]-1] } return(x) }) ## Works x <- 1:10; x[sample(10,3)] <- NA print(cbind(x, nan2last(x))) ## Doesn't work x <- seq(as.Date("2011-01-01"), as.Date("2011-01-31"), "days")
[R] Aligning Grid Lines with Date Ticks in Trellis Plot
I know this issue has been discussed before, but I hoped the advent of pretty.Date would resolve it. In the following code, the first plot produces misaligned vertical grid lines, while in the second plot they are properly aligned. Is there any way to get something along the lines of the first call to produce properly aligned grid lines? X <- data.frame(date=seq(as.Date("1990/01/01"), as.Date("2010/11/24"), by=1)) X$value <- rnorm(length(X$date)) ## This produces grid lines not aligned with the date ticks xyplot(value ~ date, data=X, panel = function(...) { panel.grid(v=-1,h=-1,col="black") panel.xyplot(...) }) ## This produces grid lines aligned with the date ticks xyplot(value ~ date, data=X, panel = function(...) { panel.abline(v=pretty(X$date, 5)) panel.grid(v=0,h=-1,col="black") panel.xyplot(...) }) Thanks. - Elliot __ R-help@r-project.org 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.
[R] Multiple Line Plots with xyplot
I'm trying to make multiple line plots, each with a different color, using the xyplot command. Specifically, I have an NxK matrix Y and an Nx1 matrix x. I would like the plot to contain a line for each (x, Y[,i]), i=1:K. I know something like xyplot(Y[,1] + Y[,2] + Y[,3] ~ x, type='l') will work, but if Y is large, this notation can get very awkward. Is there a way to do something simpler, along the lines of xyplot(Y ~ x, type='l') Thanks. - Elliot -- Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC 134 Mount Auburn Street | Cambridge, MA | 02138 Phone: (617) 503-4619 | Email: elliot.bernst...@fdopartners.com __ R-help@r-project.org 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.