Vectors are 1D collections of the same scalar type.
xs <- c(1, 0.5, 0.25)
ss <- c('G', 'A', 'T', 'T', 'A', 'C', 'A')
bs <- c(T, T, F, F, T, T, F, F)
xs
ss
bs
xs[1]
Like vecorrs, only in 2D (matrices) or more (arrays).
m <- matrix(1:12, ncol=4)
m
1 | 4 | 7 | 10 |
2 | 5 | 8 | 11 |
3 | 6 | 9 | 12 |
m[6:10]
m[m < 10]
m[2,]
m[,2]
Try to solve the following problems without searching the web. You can
use the built-in help()
function.
Create the following \(3 \times 3\) matrix and save in a variable
called A
.
What is the sum of all the numbers in A?
Create a vector of the column sums in A
using the colSums
function.
Create a vector of the row sums in A
using the apply
function.
What is the sum of the numbers in bottom right \(2 \times2\) block (i.e the numbers 2, 3, 8, 9)
ls <- list(dna=ss, ispurine=ss %in% c('A', 'G'))
ls
ls$dna
class(ls$dna)
ls[[1]]
class(ls[[1]])
A data frame is a special list of vectors where all the vectors have the same length. Because all the vectors have the same length, it can also be thought of as a 2D table or matrix and manipulated in the same way.
df <- as.data.frame(ls)
class(ls)
class(df)
df
dna | ispurine | |
---|---|---|
1 | G | TRUE |
2 | A | TRUE |
3 | T | FALSE |
4 | T | FALSE |
5 | A | TRUE |
6 | C | FALSE |
7 | A | TRUE |
df[4:6, ]
dna | ispurine | |
---|---|---|
4 | T | FALSE |
5 | A | TRUE |
6 | C | FALSE |
df$ispurine
df[df$ispurine, ]
dna | ispurine | |
---|---|---|
1 | G | TRUE |
2 | A | TRUE |
5 | A | TRUE |
7 | A | TRUE |
gender <- c('M', 'M', 'F', 'F', 'M', 'F', 'M')
height <- c(1.65, 1.82, 1.56, 1.66, 1.72, 1.6, 1.8)
weight <- c(65, 102, 55, 46, 78, 60, 72)
bods <- data.frame(gender, height, weight)
bods
gender | height | weight | |
---|---|---|---|
1 | M | 1.65 | 65 |
2 | M | 1.82 | 102 |
3 | F | 1.56 | 55 |
4 | F | 1.66 | 46 |
5 | M | 1.72 | 78 |
6 | F | 1.6 | 60 |
7 | M | 1.8 | 72 |
We can add a new calculated column easily. Let’s include the body mass index (bmi).
bods$bmi <- bods$weight/bods$height^2
bods
gender | height | weight | bmi | |
---|---|---|---|---|
1 | M | 1.65 | 65 | 23.87511 |
2 | M | 1.82 | 102 | 30.79338 |
3 | F | 1.56 | 55 | 22.60026 |
4 | F | 1.66 | 46 | 16.69328 |
5 | M | 1.72 | 78 | 26.3656 |
6 | F | 1.6 | 60 | 23.4375 |
7 | M | 1.8 | 72 | 22.22222 |
Let’s get rid of the bmi column.
bods$bmi <- NULL
bods
gender | height | weight | |
---|---|---|---|
1 | M | 1.65 | 65 |
2 | M | 1.82 | 102 |
3 | F | 1.56 | 55 |
4 | F | 1.66 | 46 |
5 | M | 1.72 | 78 |
6 | F | 1.6 | 60 |
7 | M | 1.8 | 72 |
How many males are there?
What is the mean height?
What is the mean weight for femalse?
A person is classified as obese if their BMI exceeds 30. Add the BMI
column back into the data frame, as well as a new logical column
is.obese
indicating if a person is obese or not.