We just learned about the c() operator, which forms a vector from its arguments. If we're trying to build a vector containing a sequence of numbers, there are several useful functions at our disposal. These are the colon operator : and the sequence function seq() .
We'll look at data frame and plotting in much more detail in later classes. For a previous of what's to come, here's a very basic example.
For this example we'll use a very simple dataset. The cars data comes with the default installation of R. To see the first few columns of the data, just type head(cars) .
head(cars)
We'll do a bad thing here and use the attach() command, which will allow us to access the speed and dist columns of cars as though they were vectors in our workspace.
attach(cars)# Using this command is poor style. We will avoid it in the future.
speed
dist
# (a) Calculate the average and standard deviation of speed and distance.
mean(speed)# average of speed
mean(dist)# average of distance
sd(speed)# standard deviation of speed
sd(dist)# standard deviation of distance
We can easily produce a histogram of stopping distance using the qplot function.
qplot(dist, bins=40)# Histogram of stopping distance
# (b) Produce a histogram of stopping distance using the hist function with 10 bins.
hist(dist, breaks = seq(min(dist), max(dist), l=11),col ="pink", main ="Histogram of Stopping Distance",xlab="Distance")
The qplot(x,y,...) function can also be used to plot a vector y against a vector x . You can type ?qplot into the Console to learn more about the basic qplot function.
# (c) Use the qplot(x,y) function to create a scatterplot of dist against speed.
qplot(speed, dist)
# (d) Use the boxplot function to create a boxplot of speed.