P01. Introduction to R, part 1: solutions

A. Create simple objects of different types in the workspace

item1 <- 1
item2 <- "a"
item3 <- 3.78901

B. Objects with more than 1 entry are called vectors

# create some vectors using c() (short for concatenate)
# all items of a vector must be the same class

object1 <- c("a", "b", "c")
object2 <- 1:3
object3 <- c(1.3, -4.5, 6.99)

# you can create vectors using other named items or objects
object4 <- c(item1, item2, item3)

# take a look
object1
[1] "a" "b" "c"
object2
[1] 1 2 3
object3
[1]  1.30 -4.50  6.99
object4
[1] "1"       "a"       "3.78901"

C. Look for your objects in the environment tab (upper right)

D. What class of objects is each object?
Answer: 1. character, 2. integer, 3. numeric, 4. character

# use class() to find this
class(object1)
[1] "character"
class(object2)
[1] "integer"
class(object3)
[1] "numeric"
class(object4)
[1] "character"

Is object 4 the class you expected?
Answer: No, it has been converted (cast) to a character this is because it is a combination of different types.

E. You can manipulate objects and make operations on them

object2 + 1
[1] 2 3 4
object3*object3
[1]  1.6900 20.2500 48.8601
object2/object3
[1]  0.7692308 -0.4444444  0.4291845
# object1 + object2

what happened at each of these commands? is it what you expected?

Answer: here was an error for the last one, because characters cannot be added.

F. You can view the history of commands that were run.

Check the history of commands in the history tab (upper right).

G. Make some other objects

df1 <- data.frame(ID=1:5,
animal=c("bear", "cat", "horse", "cat", "pig"),
weight=c(200, 5, 600, 8, 100))
mat1 <- matrix(data=1:50, nrow=10, ncol=5)

# view these objects
df1
  ID animal weight
1  1   bear    200
2  2    cat      5
3  3  horse    600
4  4    cat      8
5  5    pig    100
mat1
      [,1] [,2] [,3] [,4] [,5]
 [1,]    1   11   21   31   41
 [2,]    2   12   22   32   42
 [3,]    3   13   23   33   43
 [4,]    4   14   24   34   44
 [5,]    5   15   25   35   45
 [6,]    6   16   26   36   46
 [7,]    7   17   27   37   47
 [8,]    8   18   28   38   48
 [9,]    9   19   29   39   49
[10,]   10   20   30   40   50
# View(df1)
# View(mat1)

In R, what is the difference between a data.frame and a matrix? hint: google it.

Answer: Matrices can be only of 1 class, e.g. numeric, integer, etc. Data frames can be a mixture of classes by column, e.g. col1=numeric, col2=character. View will let you open seperate windows to view the dataframe and matrice in R.

H. Calculate the mean weight of the animals in df1. (i.e. mean of column 3 of df1)

# you can either refer to a column by name or by index
# check what the names are
colnames(df1)
[1] "ID"     "animal" "weight"
# reference a column by name
df1$weight
[1] 200   5 600   8 100
# check how to use the function "mean"
# ? allows you to view the help file of any inbuilt function
# ?mean

# calculate the mean
mean(df1$weight)
[1] 182.6
mean(df1[, 3])
[1] 182.6

Answer: 182.6. ?mean will open up the helpfile of the function mean in the Help window.

I. does mat1 have column names at this point?
Answer: No. But we can assign them.

# set column names for mat1
colnames(mat1) <- c("A", "B", "C", "D", "E")
mat1
       A  B  C  D  E
 [1,]  1 11 21 31 41
 [2,]  2 12 22 32 42
 [3,]  3 13 23 33 43
 [4,]  4 14 24 34 44
 [5,]  5 15 25 35 45
 [6,]  6 16 26 36 46
 [7,]  7 17 27 37 47
 [8,]  8 18 28 38 48
 [9,]  9 19 29 39 49
[10,] 10 20 30 40 50

J. What is the value of the element row 5, column C in mat1? what is the value of row 8, column E?

# inspect the element by referencing the row, then column, either by name or number
mat1[5, "C"]
 C 
25 
mat1[5, 3]
 C 
25 
mat1[8, "E"]
 E 
48 

Answer: 25, 48

K. Discard all of mat1 except the 1st and 4th column.

# subset mat1 to keep only the 1st and 4th column by number or name
mat1[ , c(1, 4)]
       A  D
 [1,]  1 31
 [2,]  2 32
 [3,]  3 33
 [4,]  4 34
 [5,]  5 35
 [6,]  6 36
 [7,]  7 37
 [8,]  8 38
 [9,]  9 39
[10,] 10 40
mat1[ , c("A", "D")]
       A  D
 [1,]  1 31
 [2,]  2 32
 [3,]  3 33
 [4,]  4 34
 [5,]  5 35
 [6,]  6 36
 [7,]  7 37
 [8,]  8 38
 [9,]  9 39
[10,] 10 40
# Create a new object of the smaller matrix.
# assign the subsetted version as a new object, mat2
mat2 <- mat1[ , c("A", "D")]
mat2 <- mat1[ , c(1, 4)]