The Art of R Programming

(WallPaper) #1
graph, and the male and female plots pretty much coincide. (It does appear
that males have more variability, though.) This is a common issue in statisti-
cal graphics. A finer graphical analysis may be more illuminating, but at least
here we see evidence of the strong correlation and that the relation does not
vary much across genders.

Figure 2-1: Abalone diameter vs. length by gender

We can compact the plotting code in the previous example by yet
another use ofifelse. This exploits the fact that the plot parameterpch
is allowed to be a vector rather than a single character. In other words, R
allows us to specify a different plot character for each point.

pchvec <- ifelse(aba$Gender == "M","o","x")
plot(aba$Length,aba$Diameter,pch=pchvec)

(Here, we’ve omitted the recoding to 1, 2, and 3, but you may wish to retain
it for various reasons.)

2.10 Testing Vector Equality......................................................


Suppose we wish to test whether two vectors are equal. The naive approach,
using==, won’t work.

>x<-1:3
> y <- c(1,3,4)
>x==y
[1] TRUE FALSE FALSE

54 Chapter 2

Free download pdf