In previous years, the lecture portion of the course did not include any training in R. Many students, especially students in programs other than ECo do not enroll in the lab and therefore do not learn any R.
After considering feedback from students, faculty, and the general need to learn R for academic and professional careers I have decided to include some basic R in the lecture.
I feel that students who only enroll in the lecture are not served well by learning about data analysis without learning to perform any of the techniques in R.
Students in the lab course will gain much more experience and learn more advanced techniques, but the lecture will now include template R code so everyone can have some hands-on experience.
<-
and
=
You can assign values to variables using two different operators:
<-
=
You are likely to encounter both. They assign values to variables in slightly different ways, but the difference is only relevant in advanced R programming. THe difference won’t matter in this course.
You’ll find people who are passionate about using one or the other. I
personally prefer to use the equals symbol, =
because it is
more consistent with syntax in other languages that I work in such as
Java and C. I also like to reserve the <-
symbol for
occasions when it truly is needed instead of =
It’s really a matter of style, and I don’t think it’s worth fighting over!
Be prepared to see both symbols. You may use either symbol in your coding work.
c(1, 2, 3)
str(my_vec)
<-
and/or =
names(my_vec)
sum(my_vec)
>
, >
,
>=
, <=
, ==
,
!=
my_vec[3]
my_vec[c(1, 2, 4)]
2:5
my_vec["element_1"]
mean(my_vec)
matrix()
cbind()
and rbind()
rowSums()
and colSums()
summary()
- summary performs different things on
different types of objectshead()
and tail()
data.frame()
$
subset()
)order()
sort()
c()
matrix()
data.frame()
list()
array()
- Note: we probably won’t use this function in
the course, but you should know that it exists.Create:
a
that contains the text of your first
name.b1
that contains the number 45.6b2
that contains the text “45.6”c1
that contains the sequence of integers
from 0 to 3a
?b1
?b2
?b1
and b2
and why.b1
and
c1
the same type? Why or why not?b1
and c1
. Consider both the number of
elements in each variable and the data types.Create a vector called v1 that contains a sequence of integers from -2 to 2.
When you print the contents of v1, it should look like this:
## [1] -2 -1 0 1 2
Now, use v1 to create a new vector called v2 whose elements are the elements of v1 multiplied by 3. It should look like this:
## [1] -6 -3 0 3 6
Finally, calculate the sum of all the elements in v2.
v1
.v2
.v2
.Do you remember the byrow argument to the matrix() function?
Create a vector called vec_4
whose elements are the
integers from 1 to 12.
Create a matrix mat_1
from vec_4
that has
three rows and four columns. The values in mat_1
should be
sequentially increasing by row.
mat_1
should contain the
values 1, 2, 3, 4.Create a matrix mat_2
from vec_4
that has
three rows and four columns. The values in mat_2
should be
sequentially increasing by column.
mat_1
should contain
the values 1, 2, 3.mat_1
.mat_2
.Create a list, named my_list_1
with following three
elements:
Name the elements in my_list_1
:
Make sure the elements in your list are in the order specified. (look at the names closely)
Hint: remember the subsetting operators [[]] and $?
my_list_1
.Run the following code to build a vector called my_vec
and print its contents:
my_vec = rep(1:3, 5)
my_vec
## [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
Use the logical equality test operator == to create a vector,
my_bool_vec
, of Boolean values from
my_vec
.
my_bool_vec
should be the same length as
my_vec
.my_bool_vec
should have TRUE
values in the
positions where my_vec
has values of 3.You can run the following code to check that you have the correct
values in my_bool_vec
:
data.frame(my_vec, my_bool_vec)
## my_vec my_bool_vec
## 1 1 FALSE
## 2 2 FALSE
## 3 3 TRUE
## 4 1 FALSE
## 5 2 FALSE
## 6 3 TRUE
## 7 1 FALSE
## 8 2 FALSE
## 9 3 TRUE
## 10 1 FALSE
## 11 2 FALSE
## 12 3 TRUE
## 13 1 FALSE
## 14 2 FALSE
## 15 3 TRUE
Use my_bool_vec
to retrieve all of the elements of
my_vec
that have a value of 3.
Hint: Use the square bracket subsetting operator: [].
Your result should look like this:
## [1] 3 3 3 3 3
my_bool_vec
.my_vec
using my_bool_vec
.