Biology Reference
In-Depth Information
generated by the
plot
function.
plot
is a generic function for data visualization
that chooses a suitable plot depending on the data, in this case, barplots for factors.
The
main
argument specifies the title of each plot, while
xlab
and
ylab
specify
the labels to the horizontal and vertical axes, respectively.
Exploring numeric data requires many of the
R
functions illustrated above for
categorical data. According to the description of the
lizards
data provided in
Schoener
(
1968
), a branch is classified as
narrow
if its diameter is lesser or equal
than 4 inches and
wide
otherwise. For the sake of the example, we can generate
some random values according to these specifications and associate them with the
Species
.
> diam = numeric(length = nrow(lizards))
> narrow = (lizards$Diameter == "narrow")
> wide = (lizards$Diameter == "wide")
> diam[narrow] = runif(n = 252, min = 2, max = 4)
> diam[wide] = runif(n = 157, min = 4, max = 6)
> new.data = data.frame(
+ Species = lizards[, "Species"],
+ Sim.Diameter = diam)
First, we create a new vector called
diam
with one entry for each observation (the
number of rows (
nrow
)of
lizards
). Then we create two logical vectors with
indicator variables identifying which branches are
narrow
and which are
wide
and use it to correctly assign the generated random diameters. The
runif
function
generates independent random values from a uniform distribution in the range
(
2
,
4
)
for
narrow
branches and in
for
wide
branches. The (now populated) vector
diam
is then stored as
Sim.Diameter
in a new data frame called
new.data
along with the
Species
variable from the original data.
The behavior of
Sim.Diameter
can again be examined using
summary
,
which in this case reports the mean and the quantiles of the new variable.
(
4
,
6
)
> summary(new.data[, "Sim.Diameter"])
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.000 2.855 3.641 3.759 4.640 5.982
It is also interesting to investigate how the diameter differs between the two
Species
, both in location (with
summary
again) and variability (with
var
).
> is.sagrei = (new.data[, "Species"] == "Sagrei")
> summary(new.data[is.sagrei, "Sim.Diameter"])
Min. 1st Qu. Median
Mean 3rd Qu.
Max.
2.000
2.777
3.467
3.566
4.190
5.965
> summary(new.data[!is.sagrei, "Sim.Diameter"])
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.035 2.903 3.807 3.888 4.838 5.982
> var(new.data[is.sagrei, "Sim.Diameter"])
[1] 1.038415
Search WWH ::
Custom Search