Hi: Your intention isn't crystal clear to me, but I'll give it a shot...
On Mon, Jan 17, 2011 at 10:48 PM, Ben Harrison < [email protected]> wrote: > hello, I am very new to R. > My current data set is a mix of values and categories. It is a geoscience > data set, with values per rock sample. Case in point, each sample belongs > to > a lithology class, and each sample has several physical property > measurements (density, porosity...). > > I want to be able to plot these physical properties for all samples in each > lithology class. this is how i'm doing it now: > > > tc = read.table(....) > > attach(tc) > > names(tc) > tc = [1] "Well" "Depth" "Latitude" > "Longitude" > [5] "Formation" "Lithology" "LithClass" "CondUncert" > [9] "sample.ID" "Conductivity" "Density" "Porosity" > > > plot(Depth[LithClass=='sand'], Conductivity[LithClass=='sand']) > (ad nauseum... how can I loop through them all?) > One way: sand <- subset(tc, LithClass == 'sand') plot(Conductivity ~ Depth, data = sand, ...) Re the parenthesized statement: loop through all of what? The pairs of variables within a data subset, the data subsets, or both? If you made it clear what you were really after, one could be more forthcoming, but if the idea is to plot all pairs of variables within a particular data subset, there are functions in the apply family and in package plyr that can help you in that regard. Here's one approach for the individual boxplots, applied to the clay LithClass: vars <- names(which(sapply(tc, is.numeric))) # select numeric vars clay <- subset(tc, LithClass == 'clay') # Using the plot function g <- function(x) { with(clay, boxplot(eval(parse(text = x)), xlab = eval(substitute(x)))) Sys.sleep(1) } # (1): loop for(i in seq_along(vars)) g(vars[i]) # (2): Use lapply in place of the loop: lapply(vars, g) # For all x-y scatterplots, one approach is via the m_ply() function # in package plyr. A more efficient means of choosing x and y in # expand.grid() is certainly possible, though. library(plyr) vpairs <- expand.grid(x = vars, y = vars) vpairs <- vpairs[vpairs$x != vpairs$y, ] # Basic plot function: f <- function(x, y) { fm <- as.formula(paste(y, x, sep = '~')) plot(fm, data = clay) Sys.sleep(1) } # Let the show begin: m_ply(vpairs, f) It's easier to do all of this when the plot function has a formula interface. Of course, the pairs() function or one of its variants among multiple packages may well be a more efficient approach. If you can split the data into multiple subsets, you can wrap up some or all of the above in a function and call it with lapply() in the base package. I'll leave that as a homework exercise :) Then there's the issue of saving them... HTH, Dennis and ... > > > boxplot(Conductivity[LithClass=='clay']) > > boxplot(Conductivity~LithClass) # whole set of boxplots on one > diagram, but > # what if want to exclude one or two of the LithClasses? > > and ... > > > pairs(c(tc[10],tc[2],tc[11],tc[12])) > this is as advanced as I've got. > > Any tips would be greatly appreciated. > > Ben. > > [[alternative HTML version deleted]] > > ______________________________________________ > [email protected] 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]] ______________________________________________ [email protected] 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.

