[R] table, xyplot, names, & loops

Erik Iverson eiverson at NMDP.ORG
Tue Aug 25 21:23:37 CEST 2009


And of course I did not test this :).  Within the data.frame argument list, please change the <- operators to = signs.  Then it should work.

Erik 

-----Original Message-----
From: Erik Iverson 
Sent: Tuesday, August 25, 2009 1:17 PM
To: 'w_poet'; r-help at r-project.org
Subject: RE: [R] table, xyplot, names, & loops

Hello, 

"I'm just starting out in R and have a basic question about xyplot and
tables. Suppose I had a table of data with the following names: Height,
Age_group, City. I'd like to plot mean Height vs Age_group for each City"

You did not provide a sample data.frame, so I generated one.  This example is basically borrowed directly from Figure 4.3 in Sarkar's excellent book, "Lattice". Personally, I do feel that a plot such as the one below is a better display choice than a simple table of the means, but some may disagree. 

Please note that my random data do not contain effects for either age.group or city, so my guess is that your resulting plot will look cleaner (i.e., contain some visual signal.) 

## BEGIN SAMPLE R CODE

## create sample data.frame
df <- data.frame(height <- rnorm(1000, 10),
                 age.group <- sample(gl(10,100,
                                        labels = paste("Age Group", 1:10))),
                 city <- sample(gl(4, 250, labels = paste("City", 1:4))))

## tabulate the data in matrix form
h.tab <- with(df, tapply(height,
                         list(age.group, city),
                         mean))

## use dotplot with the matrix object 
dotplot(h.tab, type = "o",
        auto.key = list(lines = TRUE, space = "right"),
        xlab = "height")

## END SAMPLE R CODE

Best,
Erik Iverson 




More information about the R-help mailing list