R Coding Fundamentals


These are the solutions for the Week 1 Computer Lab.


Preparation

No answer required.

1 Exercises

1.1

a <- 9
a
## [1] 9

1.2

is.numeric(a)
## [1] TRUE

The output of TRUE tells us that yes, indeed, a is a numeric variable.

1.3

b <- 8

1.4

c <- sqrt(a * b)
c
## [1] 8.485281
is.numeric(c)
## [1] TRUE

1.5

No answer required.

1.6

round(c, digits = 3)
## [1] 8.485

1.7

fives <- seq(from = 0, to = 40, by = 5) 
fives
## [1]  0  5 10 15 20 25 30 35 40

1.8

countries <- c("Australia", "Singapore", "China", "New Zealand", "Japan", "India")
countries
## [1] "Australia"   "Singapore"   "China"       "New Zealand" "Japan"      
## [6] "India"

1.9

pop.millions <- c(25, 5, 1433, 4, 126, 1366)

1.10

matrix.name <- cbind(fives, pop.millions)
## Warning in cbind(fives, pop.millions): number of rows of result is not a
## multiple of vector length (arg 2)
matrix.name
##       fives pop.millions
##  [1,]     0           25
##  [2,]     5            5
##  [3,]    10         1433
##  [4,]    15            4
##  [5,]    20          126
##  [6,]    25         1366
##  [7,]    30           25
##  [8,]    35            5
##  [9,]    40         1433

Note that here, because the two vectors are not the same length, some of the pop.millions values are repeated.

1.11

dim(matrix.name) # check dimensions
## [1] 9 2
nrow(matrix.name) # check number of rows
## [1] 9
ncol(matrix.name) # check number of columns
## [1] 2

1.12

countries.df <- data.frame(countries, pop.millions)
countries.df
##     countries pop.millions
## 1   Australia           25
## 2   Singapore            5
## 3       China         1433
## 4 New Zealand            4
## 5       Japan          126
## 6       India         1366

1.13

my.list <- list(c = c, fives = fives, countries =countries, 
countries.df = countries.df)
my.list
## $c
## [1] 8.485281
## 
## $fives
## [1]  0  5 10 15 20 25 30 35 40
## 
## $countries
## [1] "Australia"   "Singapore"   "China"       "New Zealand" "Japan"      
## [6] "India"      
## 
## $countries.df
##     countries pop.millions
## 1   Australia           25
## 2   Singapore            5
## 3       China         1433
## 4 New Zealand            4
## 5       Japan          126
## 6       India         1366
names(my.list)
## [1] "c"            "fives"        "countries"    "countries.df"

1.14

library(MASS)

1.15

?MASS # or
help(MASS)


That’s all the questions covered. If there were any parts you were unsure about, take a look back over the relevant sections of the Introduction to R material.


These notes have been prepared by Rupert Kuveke and Amanda Shaker. The copyright for the material in these notes resides with the authors named above, with the Department of Mathematics and Statistics and with La Trobe University. Copyright in this work is vested in La Trobe University including all La Trobe University branding and naming. Unless otherwise stated, material within this work is licensed under a Creative Commons Attribution-Non Commercial-Non Derivatives License BY-NC-ND.