1 Introduction

Vectors hold one type in one dimension. Real data needs bigger containers:

Container Holds Example
matrix one type, rows × columns a plate of absorbance readings
data frame one type per column a patient table
list anything everything about one gene

In this lab you will:

  • create matrices, data frames and lists,
  • select values by position, name and condition,
  • calculate on rows and columns,
  • filter, extend and merge data frames,
  • deal with missing values,
  • get results out of lists.

As always: predict first, then run. If the output surprises you, find out why. Click the key to reveal example code. Keep your code and notes in your R Markdown learning journal.

2 Matrices

A 96-well plate is a matrix: rows A–H, columns 1–12. Here is a small part of one, with absorbance readings for 3 rows and 4 columns, read row by row:

readings <- c(0.12, 0.45, 0.88, 0.05,
              0.15, 0.51, 0.93, 0.06,
              0.11, 0.48, 0.85, 0.05)
  1. Create a matrix plate from readings with 3 rows, filled row by row. Name the rows "A", "B", "C" and the columns "1" to "4". What are its dimensions? What does length() return?

plate <- matrix(readings, nrow = 3, byrow = TRUE)
rownames(plate) <- c("A", "B", "C")
colnames(plate) <- 1:4
plate
dim(plate)
length(plate)   # total number of values
##      1    2    3    4
## A 0.12 0.45 0.88 0.05
## B 0.15 0.51 0.93 0.06
## C 0.11 0.48 0.85 0.05
## [1] 3 4
## [1] 12

What happens if you leave out byrow = TRUE?

matrix(readings, nrow = 3)   # filled column by column: the values end up in the wrong wells
##      [,1] [,2] [,3] [,4]
## [1,] 0.12 0.05 0.93 0.48
## [2,] 0.45 0.15 0.06 0.85
## [3,] 0.88 0.51 0.11 0.05
  1. Select:
    • the reading in well B3,
    • all of row C,
    • all of column 2,
    • all readings above 0.5.

plate["B", "3"]
plate["C", ]
plate[, "2"]
plate[plate > 0.5]
## [1] 0.93
##    1    2    3    4 
## 0.11 0.48 0.85 0.05 
##    A    B    C 
## 0.45 0.51 0.48 
## [1] 0.51 0.88 0.93 0.85
  1. Column 4 is the blank. Subtract the mean of the blank wells from every reading. Then calculate the mean of each column and the maximum of each row.

blank <- mean(plate[, "4"])
plate_corrected <- plate - blank
round(plate_corrected, 3)
colMeans(plate_corrected)
apply(plate_corrected, 1, max)
##       1     2     3      4
## A 0.067 0.397 0.827 -0.003
## B 0.097 0.457 0.877  0.007
## C 0.057 0.427 0.797 -0.003
##             1             2             3             4 
##  7.333333e-02  4.266667e-01  8.333333e-01 -2.312965e-18 
##         A         B         C 
## 0.8266667 0.8766667 0.7966667
  1. Add a row "D" with the readings 0.13, 0.47, 0.90, 0.05. Then transpose the plate. What happened to the row and column names?

plate <- rbind(plate, D = c(0.13, 0.47, 0.90, 0.05))
plate
t(plate)   # rows and columns, including their names, swap places
##      1    2    3    4
## A 0.12 0.45 0.88 0.05
## B 0.15 0.51 0.93 0.06
## C 0.11 0.48 0.85 0.05
## D 0.13 0.47 0.90 0.05
##      A    B    C    D
## 1 0.12 0.15 0.11 0.13
## 2 0.45 0.51 0.48 0.47
## 3 0.88 0.93 0.85 0.90
## 4 0.05 0.06 0.05 0.05
  1. Try to put the text "empty" in well A4. What happens to the whole matrix?

plate_test <- plate
plate_test["A", "4"] <- "empty"
plate_test       # every value becomes text: a matrix holds one type
##   1      2      3      4      
## A "0.12" "0.45" "0.88" "empty"
## B "0.15" "0.51" "0.93" "0.06" 
## C "0.11" "0.48" "0.85" "0.05" 
## D "0.13" "0.47" "0.9"  "0.05"

3 Data frames

A small clinical study:

patients <- data.frame(
  id     = c("P01", "P02", "P03", "P04", "P05", "P06"),
  age    = c(34, 58, 47, 62, 29, 51),
  sex    = c("F", "M", "M", "F", "F", "M"),
  bmi    = c(22.4, 31.2, 27.8, 29.5, 21.0, 33.1),
  smoker = c(FALSE, TRUE, FALSE, TRUE, FALSE, FALSE),
  group  = c("drug", "placebo", "drug", "placebo", "placebo", "drug")
)
  1. How many rows and columns does patients have? What type is each column? Get a summary of every column.

dim(patients)
str(patients)
summary(patients)
## [1] 6 6
## 'data.frame':    6 obs. of  6 variables:
##  $ id    : chr  "P01" "P02" "P03" "P04" ...
##  $ age   : num  34 58 47 62 29 51
##  $ sex   : chr  "F" "M" "M" "F" ...
##  $ bmi   : num  22.4 31.2 27.8 29.5 21 33.1
##  $ smoker: logi  FALSE TRUE FALSE TRUE FALSE FALSE
##  $ group : chr  "drug" "placebo" "drug" "placebo" ...
##       id                 age            sex                 bmi       
##  Length:6           Min.   :29.00   Length:6           Min.   :21.00  
##  Class :character   1st Qu.:37.25   Class :character   1st Qu.:23.75  
##  Mode  :character   Median :49.00   Mode  :character   Median :28.65  
##                     Mean   :46.83                      Mean   :27.50  
##                     3rd Qu.:56.25                      3rd Qu.:30.77  
##                     Max.   :62.00                      Max.   :33.10  
##    smoker           group          
##  Mode :logical   Length:6          
##  FALSE:4         Class :character  
##  TRUE :2         Mode  :character  
##                                    
##                                    
## 
  1. Get:
    • the bmi column, with $ and with [ , ],
    • the rows of patients 2 to 4,
    • the age of the third patient,
    • the mean age.

patients$bmi
patients[, "bmi"]
patients[2:4, ]
patients[3, "age"]
mean(patients$age)
## [1] 22.4 31.2 27.8 29.5 21.0 33.1
## [1] 22.4 31.2 27.8 29.5 21.0 33.1
##    id age sex  bmi smoker   group
## 2 P02  58   M 31.2   TRUE placebo
## 3 P03  47   M 27.8  FALSE    drug
## 4 P04  62   F 29.5   TRUE placebo
## [1] 47
## [1] 46.83333
  1. Filter the table:
    • all smokers,
    • patients in the drug group who are older than 45,
    • the IDs of patients with a BMI of 30 or more.

patients[patients$smoker, ]
patients[patients$group == "drug" & patients$age > 45, ]
patients$id[patients$bmi >= 30]
##    id age sex  bmi smoker   group
## 2 P02  58   M 31.2   TRUE placebo
## 4 P04  62   F 29.5   TRUE placebo
##    id age sex  bmi smoker group
## 3 P03  47   M 27.8  FALSE  drug
## 6 P06  51   M 33.1  FALSE  drug
## [1] "P02" "P06"
  1. What is the mean BMI in the drug group? And in the placebo group?

mean(patients$bmi[patients$group == "drug"])
mean(patients$bmi[patients$group == "placebo"])
## [1] 27.76667
## [1] 27.23333
  1. Add two columns:
    • obese: TRUE if the BMI is 30 or more,
    • age_months: the age in months.
    Then make group a factor with "placebo" as the first level.

patients$obese <- patients$bmi >= 30
patients$age_months <- patients$age * 12
patients$group <- factor(patients$group, levels = c("placebo", "drug"))
levels(patients$group)
patients
## [1] "placebo" "drug"   
##    id age sex  bmi smoker   group obese age_months
## 1 P01  34   F 22.4  FALSE    drug FALSE        408
## 2 P02  58   M 31.2   TRUE placebo  TRUE        696
## 3 P03  47   M 27.8  FALSE    drug FALSE        564
## 4 P04  62   F 29.5   TRUE placebo FALSE        744
## 5 P05  29   F 21.0  FALSE placebo FALSE        348
## 6 P06  51   M 33.1  FALSE    drug  TRUE        612
  1. Blood glucose was measured in a separate lab. Their table is in a different order, is missing one patient, and has one patient who is not in your study:
glucose <- data.frame(id = c("P04", "P01", "P06", "P02", "P03", "P07"),
                      glucose = c(6.1, 5.2, 7.4, 5.9, NA, 5.5))

Merge it with patients. Which patients are in the result? Then look up the all.x argument in ?merge to keep all your patients.

merge(patients, glucose, by = "id")                # only patients in both tables
combined <- merge(patients, glucose, by = "id", all.x = TRUE)
combined                                           # P05 is kept, with glucose NA
##    id age sex  bmi smoker   group obese age_months glucose
## 1 P01  34   F 22.4  FALSE    drug FALSE        408     5.2
## 2 P02  58   M 31.2   TRUE placebo  TRUE        696     5.9
## 3 P03  47   M 27.8  FALSE    drug FALSE        564      NA
## 4 P04  62   F 29.5   TRUE placebo FALSE        744     6.1
## 5 P06  51   M 33.1  FALSE    drug  TRUE        612     7.4
##    id age sex  bmi smoker   group obese age_months glucose
## 1 P01  34   F 22.4  FALSE    drug FALSE        408     5.2
## 2 P02  58   M 31.2   TRUE placebo  TRUE        696     5.9
## 3 P03  47   M 27.8  FALSE    drug FALSE        564      NA
## 4 P04  62   F 29.5   TRUE placebo FALSE        744     6.1
## 5 P05  29   F 21.0  FALSE placebo FALSE        348      NA
## 6 P06  51   M 33.1  FALSE    drug  TRUE        612     7.4
  1. In combined, how many values are missing in each column? How many complete rows are left after na.omit()? What is the mean glucose of the patients who have a value?

colSums(is.na(combined))
nrow(na.omit(combined))
mean(combined$glucose, na.rm = TRUE)
##         id        age        sex        bmi     smoker      group      obese 
##          0          0          0          0          0          0          0 
## age_months    glucose 
##          0          2 
## [1] 4
## [1] 6.15
  1. R comes with a real life-science data set, ToothGrowth: tooth length in 60 guinea pigs given vitamin C as orange juice (OJ) or ascorbic acid (VC), at three doses. Explore it:
    • How many rows and columns? What are the column types?
    • What is the mean tooth length for each supplement?
    • Show only the rows with the highest dose (2 mg/day).

dim(ToothGrowth)
str(ToothGrowth)
mean(ToothGrowth$len[ToothGrowth$supp == "OJ"])
mean(ToothGrowth$len[ToothGrowth$supp == "VC"])
ToothGrowth[ToothGrowth$dose == 2, ]
## [1] 60  3
## 'data.frame':    60 obs. of  3 variables:
##  $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
##  $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
##  $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
## [1] 20.66333
## [1] 16.96333
##     len supp dose
## 21 23.6   VC    2
## 22 18.5   VC    2
## 23 33.9   VC    2
## 24 25.5   VC    2
## 25 26.4   VC    2
## 26 32.5   VC    2
## 27 26.7   VC    2
## 28 21.5   VC    2
## 29 23.3   VC    2
## 30 29.5   VC    2
## 51 25.5   OJ    2
## 52 26.4   OJ    2
## 53 22.4   OJ    2
## 54 24.5   OJ    2
## 55 24.8   OJ    2
## 56 30.9   OJ    2
## 57 26.4   OJ    2
## 58 27.3   OJ    2
## 59 29.4   OJ    2
## 60 23.0   OJ    2

4 Lists

  1. Create a list gene with everything you know about one gene:
    • symbol: "TP53",
    • chromosome: 17,
    • exon_lengths: c(103, 22, 279, 184, 113, 110, 137),
    • functions: c("DNA repair", "apoptosis", "cell cycle arrest"),
    • expression: a data frame with the columns tissue ("liver", "lung", "skin") and tpm (45.2, 61.8, 30.1).
    Look at it with str().

gene <- list(
  symbol       = "TP53",
  chromosome   = 17,
  exon_lengths = c(103, 22, 279, 184, 113, 110, 137),
  functions    = c("DNA repair", "apoptosis", "cell cycle arrest"),
  expression   = data.frame(tissue = c("liver", "lung", "skin"),
                            tpm    = c(45.2, 61.8, 30.1))
)
str(gene)
## List of 5
##  $ symbol      : chr "TP53"
##  $ chromosome  : num 17
##  $ exon_lengths: num [1:7] 103 22 279 184 113 110 137
##  $ functions   : chr [1:3] "DNA repair" "apoptosis" "cell cycle arrest"
##  $ expression  :'data.frame':    3 obs. of  2 variables:
##   ..$ tissue: chr [1:3] "liver" "lung" "skin"
##   ..$ tpm   : num [1:3] 45.2 61.8 30.1
  1. Compare gene["symbol"] and gene[["symbol"]]. What is the class of each?

gene["symbol"]
class(gene["symbol"])     # a list
gene[["symbol"]]
class(gene[["symbol"]])   # the value itself
## $symbol
## [1] "TP53"
## 
## [1] "list"
## [1] "TP53"
## [1] "character"
  1. Get:
    • the length of the third exon,
    • the total length of all exons,
    • the number of known functions,
    • the TPM in lung.

gene$exon_lengths[3]
sum(gene$exon_lengths)
length(gene$functions)
gene$expression$tpm[gene$expression$tissue == "lung"]
## [1] 279
## [1] 948
## [1] 3
## [1] 61.8
  1. Add an element aliases with the value c("p53", "LFS1"). Then remove chromosome. Check the names of the list.

gene$aliases <- c("p53", "LFS1")
gene$chromosome <- NULL
names(gene)
## [1] "symbol"       "exon_lengths" "functions"    "expression"   "aliases"
  1. Use t.test() to compare tooth length between the two supplements in ToothGrowth. The result is a list. What are its elements? Get the p-value and the 95% confidence interval.

res <- t.test(len ~ supp, data = ToothGrowth)
is.list(res)
names(res)
res$p.value
res$conf.int
## [1] TRUE
##  [1] "statistic"   "parameter"   "p.value"     "conf.int"    "estimate"   
##  [6] "null.value"  "stderr"      "alternative" "method"      "data.name"  
## [1] 0.06063451
## [1] -0.1710156  7.5710156
## attr(,"conf.level")
## [1] 0.95