# 1. Hello World!
Here's an R code chunk that prints the text 'Hello world!'.
# (a) Modify the code chunk below to print your name # 2. Creating sequences
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
: Colon operator:
seq(from, to, by)
seq ( 1 , 10 , 1 ) seq ( 1 , 10 , 2 ) seq ( 2 , 10 , 2 )
To learn more about a function, type
?functionname into your console. E.g.,
?seq pulls up a Help file with the R documentation for the
# (a) Use
: to output the sequence of numbers from 3 to 12
# (b) Use
seq() to output the sequence of numbers from 3 to 30 in increments of 3
# (c) Save the sequence from (a) as a variable
x , and the sequence from (b) as a variable
y . Output their product
x <- 3 : 12 y <- seq ( 3 , 30 , 3 ) x *y # 3. Cars data
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
We'll do a bad thing here and use the
attach() command, which will allow us to access the
dist columns of
cars as though they were vectors in our workspace.
# (a) Calculate the average and standard deviation of speed and distance.
mean (speed ) mean (dist ) sd (speed ) sd (dist )
We can easily produce a histogram of stopping distance using the
# (b) Produce a histogram of stopping distance using the
hist function with
hist (dist , breaks = seq (min (dist ) , max (dist ) , l = 11 ) ,col = "pink" , main = "Histogram of Stopping Distance" ,xlab = "Distance" )
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.
# (d) Use the
boxplot function to create a boxplot of speed.
boxplot (speed , horizontal = TRUE )