[R] Convert the output of by() to a data frame
I'd like to summarize several variables in a data frame, for multiple groups, and store the results in a data.frame. To do so, I'm using by(). For example: df<-data.frame(a=1:10,b=11:20,c=21:30,grp1=c("x","y"),grp2=c("x","y"),grp3=c("x","y")) dfsum<-by(df[c("a","b","c")], df[c("grp1","grp2","grp3")], range) The result has a class of "by" and a mode of "list". I'm new to R and can't find any documentation on this class, and don't see methods for it associated with the as.data.frame. How should I go about coercing this to a data frame? Is there a comprehensive source that I'm might be missing, which can tell me such things? Cheers [[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] Repeating the same calculation across multiple pairs of variables
Hi all, I frequently encounter datasets that require me to repeat the same calculation across many variables. For example, given a dataset with total employment variables and manufacturing employment variables for the years 1990-2010, I might have to calculate manufacturing's share of total employment in each year. I find it cumbersome to have to manually define a share for each year and would like to know how others might handle this kind of task. For example, given the data frame: df<-data.frame(a1=1:10, a2=11:20, a3=21:30, b1=101:110, b2=111:120, b3=121:130) I'd like to append new variables--c1, c2, and c3--to the data frame that are the result of a1/b1, a2/b2, and a3/b3, respectively. When there are only a few of these variables, I don't really have a problem, but it becomes a chore when the number of variables increases. Is there a way I can do this kind of processing using a loop? I tried defining a vector to hold the names for the "c variables" (e.g. c1,c2, ... cn) and creating new variables in a loop using code like: avars<-c("a1","a2","a3") bvars<-c("b1","b2","b3") cvars<-c("c1","c2","c3") for(i in 1:3){ df$cvars[i]<-df$avars[i]/df$bvars[i] } But the variable references don't resolve properly with this particular syntax. Any help would be much appreciated. Cheers. [[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] Preferred way to create bubble plots?
I have to create a number of bubble plots, and am wondering what methods folks prefer for this task. I've been experimenting with the symbols() function, with text() to provide plot labels. Any opinions on the relative merits of this method versus others? One criterion would be the ability to fine-tune the placement of text labels. I would like to use lattice, but haven't found a way to make it work for this purpose. Thanks in advance. [[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] How does the cex parameter scale circles?
I'm wondering how the cex parameter is used to scale circles (i.e. does it scale the radius, diameter, area, circumference, etc.?). In my case I'm using lattice with filled circles (pch=19). Based on example, it looks like R scales the radius of the circle: library(lattice) dta<-data.frame(x=rep(1,6),y=rep(1,6),sz=c(1,2,4,8,16,32)) xyplot(x~y,data=dta,col=rgb(0,0,0,50,maxColorValue=255),cex=dta$sz,pch=19) But I haven't been able to find confirmation in any R documentation, so any assistance would be much appreciated. Cheers. [[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] Adjust the distance between tick mark labels and axis
Hi all: I've been trying to figure out a way to move the tick mark labels in my lattice plots closer to the the axes. I've tinkered with a lot of the "buffer" parameters (those listed by trellis.par.get()), but haven't had any luck. Any ideas? Cheers. [[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] grImport/ghostscript problems
Hi All: I've been struggling for a while trying to get grImport up and running. I'm on a Windows 7 (home premium 64 bit) machine running R-2.12.2 along with GPL Ghostscript 9.01. I've set my Windows PATH variable to point to the Ghostscript \bin and \lib directories, and I've created the R_GSCMD environment variable pointing to gswin32c.exe. I don't have any experience with Ghostscript, but with the setup described above I can view the postscript file with the following command to the Windows command prompt: gswin32c.exe D:\Sndbx\vasarely.ps However, I can't get the PostScriptTrace() function to work on the same file. Submitting PostScriptTrace("D:/Sndbx/vasarely.ps") gives me the error: Error in PostScriptTrace("D:/Sndbx/vasarely.ps") : status 127 in running command 'gswin32c.exe -q -dBATCH -dNOPAUSE -sDEVICE=pswrite -sOutputFile=C:\Users\Al\AppData\Local\Temp\RtmppPjDAf\file5db99cb -sstdout=vasarely.ps.xml capturevasarely.ps' Your suggestions are much appreciated. Cheers, Al [[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] grImport/ghostscript problems
Paul Murrell auckland.ac.nz> writes: > > Hi > > On 28/03/2011 8:13 a.m., Al Roark wrote: > > > > Hi All: I've been struggling for a while trying to get grImport up > > and running. I'm on a Windows 7 (home premium 64 bit) machine > > running R-2.12.2 along with GPL Ghostscript 9.01. I've set my Windows > > PATH variable to point to the Ghostscript \bin and \lib directories, > > and I've created the R_GSCMD environment variable pointing to > > gswin32c.exe. I don't have any experience with Ghostscript, but with > > the setup described above I can view the postscript file with the > > following command to the Windows command prompt: gswin32c.exe > > D:\Sndbx\vasarely.ps However, I can't get the PostScriptTrace() > > function to work on the same file. Submitting > > PostScriptTrace("D:/Sndbx/vasarely.ps") gives me the error: Error in > > PostScriptTrace("D:/Sndbx/vasarely.ps") : status 127 in running > > command 'gswin32c.exe -q -dBATCH -dNOPAUSE -sDEVICE=pswrite > > -sOutputFile=C:\Users\Al\AppData\Local\Temp\RtmppPjDAf\file5db99cb > > -sstdout=vasarely.ps.xml capturevasarely.ps' Your suggestions are > > much appreciated. Cheers, Al [[alternative HTML version deleted]] > > You could try running the ghostscript command that is printed in the > error message at the Windows command prompt to see more info about the > problem (might need to remove the '-q' so that ghostscript prints > messages to the screen). > > Paul > Thanks for your reply. Perhaps this is a Ghostscript problem. When I run the Ghostscript command, I'm met with the rather unhelpful error: 'GPL Ghostscript 9.01: Unrecoverable error, exit code 1 (occurs whether or not I remove the -q)'. Interestingly, if I remove the final argument (in this case, capturevasarely.ps) the Ghostscript command executes, placing a file (appears to be xml) in the temporary directory. However, I'm not sure what to do with this result. __ 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] CairoPDF, Fonts, and Windows 7
Hi All: I have some basic questions about Cairo graphics engine. I'm trying to use the Cairo package to produce PDF output, mainly because I perceive it to be easy to use with a wide variety of fonts. But right now, I'm stuck trying to figure out what fonts are available to be used with Cairo, specifically the CairoPDF function. I've been able to successfully produce some test PDFs with grid graphics and CairoPDF (and the font "Georgia"), so my basic setup is working. I've tried to experiment with the CairoFonts() function but get the message: "the R Cairo package was not installed with fontconfig. Please consider installing the cairo graphics engine (www.cairographics.org) with freetype and fontconfig support" I'm not quite sure how to interpret this. My version of Cairo comes from the GTK+ 2.22 bundle from the GTK+ Project website. I'm running R 2.12.2 on Windows 7. Any feedback or direction toward informative resources is much appreciated. Cheers,Al [[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] Problem with grid's text-based units and TrueType fonts
Hi all: I'm using grid to create a layout in R that will include text mixed with graphics. In the layout, the positions of certain graphical elements depend on the number of lines in adjacent text blocks (which will vary from case to case). I was hoping to use grid's built in functions to automate the placement of the graphical elements. I can get this to work if I use the standard pdf device, but when using the Cairo_pdf device (from the cairoDevice package), I run into problems. I need the TrueType functionality of Cairo. The example below illustrates my problem. The rectangle in "SizeMatch.pdf" matches the size of the text block, while the rectangle in "SizeMismatch.pdf" is too small. Why is this the case? I'm using R-2.13.0 (32bit) on Windows 7. Thanks a bunch. Cheers, Al --- library(cairoDevice)library(grid) Cairo_pdf("SizeMismatch.pdf",11,8.5,pointsize=10)pushViewport(viewport(gp=gpar(fontfamily="Arial",font=1,fontsize=8,lineheight=0.9)))grid.text("The quick\nbrown fox\njumps\nover\nthe lazy dog")grid.rect(width=0.5,height=unit(5,"lines"))dev.off() pdf("SizeMatch.pdf",11,8.5,pointsize=10)pushViewport(viewport(gp=gpar(fontfamily="Times",font=1,fontsize=8,lineheight=0.9)))grid.text("The quick\nbrown fox\njumps\nover\nthe lazy dog")grid.rect(width=0.5,height=unit(5,"lines"))dev.off() [[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] Details of subassignment (for vectors and data frames)
Hi All: I'm looking to find out a bit more about how subassignment actually works and am hoping someone with knowledge of the details can fill me in (I've looked at the source code, but my knowledge of C is lacking). In the case of vectors, my reading of ?"[" would indicate that for a vector, vec <- 1:25,vec[c(1,5,25)] <- c(101,102,103)is functionally the same as indx <- c(1,5,25)for (i in 1:length(indx)) vec[indx[i]] <- c(101,102,103)[i] And in the case of a data frame, df <- data.frame(d1=1:10,d2=11:20,d3=21:30), df[c(1,5,10),c(1,3)] <- data.frame(a=101:103,b=104:106)is functionally the same asrowindx <- c(1,5,10)colindx <- c(1,3)for (i in 1:length(rowindx)) { for (j in 1:length(colindx)) df[rowindx[i],colindx[j]] <- data.frame(a=101:103,b=104:106)[i,j]} Obviously I've verified that these examples work and I realize that my loops also contain subassignments; what I'm really after is to understand the mechanics of replacing multiple elements. Is a for-loop the proper way to understand the sequential nature of subassignments here (even if it is not actually implemented using a loop)? Cheers,HR [[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] Mysterious Eclipse / StatET error messages
All, I'm not sure when this started, but I've noticed that just about any time I add to an R script in Eclipse, I get a warning in Eclipse's error log that looks like the following: Unhandled/Unknown code of R AST node: Code: 0x100 Node: <- (195, 70) Line 4 at offset 195 (can be wrong, if out of synch): All of my code appears to execute fine, but I would like to know what sort of problem this refers to. I'm running R-2.13.0 and Eclipse 3.7 in a Windows 32 bit environment. Any clues are much appreciated. Cheers. [[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.