This report is automatically generated with the R
package knitr
(version 1.5
)
.
# Chapter 7 Working in More Dimensions Adding a Second Dimension Discovering a new # dimension Creating your first matrix first.matrix <- matrix(1:12, ncol = 4) first.matrix
## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 2 5 8 11 ## [3,] 3 6 9 12
matrix(1:12, ncol = 4, byrow = TRUE)
## [,1] [,2] [,3] [,4] ## [1,] 1 2 3 4 ## [2,] 5 6 7 8 ## [3,] 9 10 11 12
### Looking at the properties str(first.matrix)
## int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
dim(first.matrix)
## [1] 3 4
length(first.matrix)
## [1] 12
attributes(my.array)
## Error: object 'my.array' not found
attr(baskets.team, "season") <- "2010-2011"
## Error: object 'baskets.team' not found
attr(baskets.team, "season")
## Error: object 'baskets.team' not found
attr(baskets.team, "season") <- NULL
## Error: object 'baskets.team' not found
## Combining vectors into a matrix baskets.of.Granny <- c(12, 4, 5, 6, 9, 3) baskets.of.Geraldine <- c(5, 4, 2, 4, 12, 9) baskets.team <- rbind(baskets.of.Granny, baskets.of.Geraldine) baskets.team
## [,1] [,2] [,3] [,4] [,5] [,6] ## baskets.of.Granny 12 4 5 6 9 3 ## baskets.of.Geraldine 5 4 2 4 12 9
cbind(1:3, 4:6, matrix(7:12, ncol = 2))
## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 2 5 8 11 ## [3,] 3 6 9 12
# Using the Indices Extracting values from a matrix Using numeric indices first.matrix[1:2, 2:3]
## [,1] [,2] ## [1,] 4 7 ## [2,] 5 8
first.matrix[2:3, ]
## [,1] [,2] [,3] [,4] ## [1,] 2 5 8 11 ## [2,] 3 6 9 12
### Dropping values using negative indices first.matrix[-2, -3]
## [,1] [,2] [,3] ## [1,] 1 4 10 ## [2,] 3 6 12
nr <- nrow(first.matrix) id <- nr * 2 + 2 first.matrix[-id]
## [1] 1 2 3 4 5 6 7 9 10 11 12
first.matrix[-(2 * nrow(first.matrix) + 2)]
## [1] 1 2 3 4 5 6 7 9 10 11 12
### Juggling dimensions first.matrix[-c(1, 3), ]
## [1] 2 5 8 11
first.matrix[2, , drop = FALSE]
## [,1] [,2] [,3] [,4] ## [1,] 2 5 8 11
## Replacing values in a matrix first.matrix[3, 2] <- 4 first.matrix
## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 2 5 8 11 ## [3,] 3 4 9 12
first.matrix[2, ] <- c(1, 3) first.matrix
## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 1 3 1 3 ## [3,] 3 4 9 12
first.matrix[1:2, 3:4] <- c(8, 4, 2, 1) first.matrix
## [,1] [,2] [,3] [,4] ## [1,] 1 4 8 2 ## [2,] 1 3 4 1 ## [3,] 3 4 9 12
# Naming Matrix Rows and Columns Changing the row and column names rownames(baskets.team) <- c("Granny", "Geraldine") rownames(baskets.team)
## [1] "Granny" "Geraldine"
colnames(baskets.team) <- c("1st", "2nd", "3th", "4th", "5th", "6th") baskets.team
## 1st 2nd 3th 4th 5th 6th ## Granny 12 4 5 6 9 3 ## Geraldine 5 4 2 4 12 9
colnames(baskets.team)[3] <- "3rd" baskets.copy <- baskets.team colnames(baskets.copy) <- NULL baskets.copy
## [,1] [,2] [,3] [,4] [,5] [,6] ## Granny 12 4 5 6 9 3 ## Geraldine 5 4 2 4 12 9
## Using names as indices baskets.team[, c("2nd", "5th")]
## 2nd 5th ## Granny 4 9 ## Geraldine 4 12
baskets.team["Granny", ]
## 1st 2nd 3rd 4th 5th 6th ## 12 4 5 6 9 3
# Calculating with Matrices Using standard operations with matrices first.matrix + 4
## [,1] [,2] [,3] [,4] ## [1,] 5 8 12 6 ## [2,] 5 7 8 5 ## [3,] 7 8 13 16
second.matrix <- matrix(1:3, nrow = 3, ncol = 4) first.matrix + second.matrix
## [,1] [,2] [,3] [,4] ## [1,] 2 5 9 3 ## [2,] 3 5 6 3 ## [3,] 6 7 12 15
# first.matrix + second.matrix[,1:3] # gives error for illustration Error in first.matrix + # second.matrix[, 1:3] : non-conformable arrays first.matrix + 1:3
## [,1] [,2] [,3] [,4] ## [1,] 2 5 9 3 ## [2,] 3 5 6 3 ## [3,] 6 7 12 15
## Calculating row and column summaries rowSums(baskets.team)
## Granny Geraldine ## 39 36
## Doing matrix arithmetic Transposing a matrix t(first.matrix)
## [,1] [,2] [,3] ## [1,] 1 1 3 ## [2,] 4 3 4 ## [3,] 8 4 9 ## [4,] 2 1 12
t(1:10)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] ## [1,] 1 2 3 4 5 6 7 8 9 10
t(first.matrix[2, ])
## [,1] [,2] [,3] [,4] ## [1,] 1 3 4 1
### Inverting a matrix square.matrix <- matrix(c(1, 0, 3, 2, 2, 4, 3, 2, 1), ncol = 3) solve(square.matrix)
## [,1] [,2] [,3] ## [1,] 0.5 -0.8333 0.1667 ## [2,] -0.5 0.6667 0.1667 ## [3,] 0.5 -0.1667 -0.1667
### Multiplying two matrices first.matrix %*% t(second.matrix)
## [,1] [,2] [,3] ## [1,] 15 30 45 ## [2,] 9 18 27 ## [3,] 28 56 84
first.matrix %*% 1:4
## [,1] ## [1,] 41 ## [2,] 23 ## [3,] 86
1:3 %*% first.matrix
## [,1] [,2] [,3] [,4] ## [1,] 12 22 43 40
# Adding More Dimensions Creating an array Using the creator functions my.array <- array(1:24, dim = c(3, 4, 2)) my.array
## , , 1 ## ## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 2 5 8 11 ## [3,] 3 6 9 12 ## ## , , 2 ## ## [,1] [,2] [,3] [,4] ## [1,] 13 16 19 22 ## [2,] 14 17 20 23 ## [3,] 15 18 21 24
### Changing the dimensions of a vector my.vector <- 1:24 dim(my.vector) <- c(3, 4, 2) identical(my.array, my.vector)
## [1] TRUE
## Using dimensions to extract values my.array[2, 3, 1]
## [1] 8
my.array[, 3, 2, drop = FALSE]
## , , 1 ## ## [,1] ## [1,] 19 ## [2,] 20 ## [3,] 21
my.array[2, , ]
## [,1] [,2] ## [1,] 2 14 ## [2,] 5 17 ## [3,] 8 20 ## [4,] 11 23
# Combining Different Types of Values in a Data Frame Creating a data frame from a matrix # Using the function as.data.frame baskets.df <- as.data.frame(t(baskets.team)) ### Looking at the structure of a data frame baskets.df
## Granny Geraldine ## 1st 12 5 ## 2nd 4 4 ## 3rd 5 2 ## 4th 6 4 ## 5th 9 12 ## 6th 3 9
str(baskets.df)
## 'data.frame': 6 obs. of 2 variables: ## $ Granny : num 12 4 5 6 9 3 ## $ Geraldine: num 5 4 2 4 12 9
### Counting values and variables nrow(baskets.df)
## [1] 6
length(baskets.df)
## [1] 2
## Creating a data frame from scratch Making a data frame from vectors employee <- c("John Doe", "Peter Gynn", "Jolie Hope") salary <- c(21000, 23400, 26800) startdate <- as.Date(c("2010-11-1", "2008-3-25", "2007-3-14")) employ.data <- data.frame(employee, salary, startdate) str(employ.data)
## 'data.frame': 3 obs. of 3 variables: ## $ employee : Factor w/ 3 levels "John Doe","Jolie Hope",..: 1 3 2 ## $ salary : num 21000 23400 26800 ## $ startdate: Date, format: "2010-11-01" "2008-03-25" ...
### Keeping characters as characters employ.data <- data.frame(employee, salary, startdate, stringsAsFactors = FALSE) str(employ.data)
## 'data.frame': 3 obs. of 3 variables: ## $ employee : chr "John Doe" "Peter Gynn" "Jolie Hope" ## $ salary : num 21000 23400 26800 ## $ startdate: Date, format: "2010-11-01" "2008-03-25" ...
## Naming variables and observations Working with variable names colnames(employ.data)
## [1] "employee" "salary" "startdate"
names(employ.data)
## [1] "employee" "salary" "startdate"
names(employ.data)[3] <- "firstday" names(employ.data)
## [1] "employee" "salary" "firstday"
### Naming observations rownames(employ.data)
## [1] "1" "2" "3"
rownames(employ.data) <- c("Chef", "BigChef", "BiggerChef") employ.data
## employee salary firstday ## Chef John Doe 21000 2010-11-01 ## BigChef Peter Gynn 23400 2008-03-25 ## BiggerChef Jolie Hope 26800 2007-03-14
# Manipulating Values in a Data Frame Extracting variables, observations, and values # Pretending it's a matrix baskets.df["3rd", "Geraldine"]
## [1] 2
baskets.df[, 1]
## [1] 12 4 5 6 9 3
str(baskets.df[, 1, drop = FALSE])
## 'data.frame': 6 obs. of 1 variable: ## $ Granny: num 12 4 5 6 9 3
### Putting your dollar where your data is baskets.df$Granny
## [1] 12 4 5 6 9 3
## Adding observations to a data frame Adding a single observation result <- rbind(baskets.df, c(7, 4)) result
## Granny Geraldine ## 1st 12 5 ## 2nd 4 4 ## 3rd 5 2 ## 4th 6 4 ## 5th 9 12 ## 6th 3 9 ## 7 7 4
baskets.df <- rbind(baskets.df, `7th` = c(7, 4)) baskets.df
## Granny Geraldine ## 1st 12 5 ## 2nd 4 4 ## 3rd 5 2 ## 4th 6 4 ## 5th 9 12 ## 6th 3 9 ## 7th 7 4
### Adding a series of new observations using rbind new.baskets <- data.frame(Granny = c(3, 8), Geraldine = c(9, 4)) rownames(new.baskets) <- c("8th", "9th") baskets.df <- rbind(baskets.df, new.baskets) ### Adding a series of values using indices baskets.df[c("8th", "9th"), ] <- matrix(c(3, 8, 9, 4), ncol = 2) baskets.df[c("8th", "9th"), ] <- c(3, 8, 9, 4) ## Adding variables to a data frame Adding a single variable baskets.of.Gabrielle <- c(11, 5, 6, 7, 3, 12, 4, 5, 9) baskets.df$Gabrielle <- baskets.of.Gabrielle head(baskets.df, 4)
## Granny Geraldine Gabrielle ## 1st 12 5 11 ## 2nd 4 4 5 ## 3rd 5 2 6 ## 4th 6 4 7
### Adding multiple variables using cbind new.df <- data.frame(Gertrude = c(3, 5, 2, 1, NA, 3, 1, 1, 4), Guinevere = c(6, 9, 7, 3, 3, 6, 2, 10, 6)) head(cbind(baskets.df, new.df), 4)
## Granny Geraldine Gabrielle Gertrude Guinevere ## 1st 12 5 11 3 6 ## 2nd 4 4 5 5 9 ## 3rd 5 2 6 2 7 ## 4th 6 4 7 1 3
# Combining Different Objects in a List Creating a list Creating an unnamed list baskets.list <- list(baskets.team, "2010-2011") baskets.list
## [[1]] ## 1st 2nd 3rd 4th 5th 6th ## Granny 12 4 5 6 9 3 ## Geraldine 5 4 2 4 12 9 ## ## [[2]] ## [1] "2010-2011"
### Creating a named list baskets.nlist <- list(scores = baskets.team, season = "2010-2011") baskets.nlist
## $scores ## 1st 2nd 3rd 4th 5th 6th ## Granny 12 4 5 6 9 3 ## Geraldine 5 4 2 4 12 9 ## ## $season ## [1] "2010-2011"
### Playing with the names of elements names(baskets.nlist)
## [1] "scores" "season"
### Getting the number of elements length(baskets.list)
## [1] 2
## Extracting elements from lists Using [[]] baskets.list[[1]]
## 1st 2nd 3rd 4th 5th 6th ## Granny 12 4 5 6 9 3 ## Geraldine 5 4 2 4 12 9
baskets.nlist[["scores"]]
## 1st 2nd 3rd 4th 5th 6th ## Granny 12 4 5 6 9 3 ## Geraldine 5 4 2 4 12 9
### Using [] baskets.list[-1]
## [[1]] ## [1] "2010-2011"
baskets.nlist[names(baskets.nlist) == "season"]
## $season ## [1] "2010-2011"
## Changing the elements in lists Changing the value of elements baskets.nlist[[1]] <- baskets.df baskets.nlist[["scores"]] <- baskets.df baskets.nlist$scores <- baskets.df baskets.nlist[1] <- list(baskets.df) baskets.list[1:2] <- list(baskets.df, "2009-2010") ### Removing elements baskets.nlist[[1]] <- NULL baskets.nlist$scores <- NULL baskets.nlist["scores"] <- NULL baskets.nlist <- list(scores = baskets.df, season = "2010-2011") baskets.nlist["scores"] <- list(NULL) baskets.nlist
## $scores ## NULL ## ## $season ## [1] "2010-2011"
### Adding extra elements using indices baskets.nlist$players <- c("Granny", "Geraldine") baskets.nlist[["players"]] <- c("Granny", "Geraldine") baskets.nlist["players"] <- list(c("Granny", "Geraldine")) baskets.list[[3]] <- c("Granny", "Geraldine") baskets.list[3] <- list(c("Granny", "Geraldine")) ### Combining lists baskets.list <- list(baskets.team, "2010-2011") players <- list(rownames(baskets.team)) c(baskets.list, players)
## [[1]] ## 1st 2nd 3rd 4th 5th 6th ## Granny 12 4 5 6 9 3 ## Geraldine 5 4 2 4 12 9 ## ## [[2]] ## [1] "2010-2011" ## ## [[3]] ## [1] "Granny" "Geraldine"
## Reading the output of str() for lists str(baskets.list)
## List of 2 ## $ : num [1:2, 1:6] 12 5 4 4 5 2 6 4 9 12 ... ## ..- attr(*, "dimnames")=List of 2 ## .. ..$ : chr [1:2] "Granny" "Geraldine" ## .. ..$ : chr [1:6] "1st" "2nd" "3rd" "4th" ... ## $ : chr "2010-2011"
## Seeing the forest through the trees
The R session information (including the OS info, R version and all packages used):
sessionInfo()
## R version 3.0.2 (2013-09-25) ## Platform: x86_64-w64-mingw32/x64 (64-bit) ## ## locale: ## [1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 ## [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C ## [5] LC_TIME=English_United Kingdom.1252 ## ## attached base packages: ## [1] stats graphics grDevices utils datasets methods base ## ## other attached packages: ## [1] BiocInstaller_1.12.1 ggplot2_0.9.3.1 reshape2_1.2.2 sos_1.3-8 ## [5] brew_1.0-6 stringr_0.6.2 knitr_1.5 plyr_1.8 ## [9] Revobase_7.1.0 RevoMods_7.1.0 RevoScaleR_7.1.0 lattice_0.20-27 ## [13] rpart_4.1-2 ## ## loaded via a namespace (and not attached): ## [1] codetools_0.2-8 colorspace_1.2-4 dichromat_2.0-0 digest_0.6.4 ## [5] evaluate_0.5.1 foreach_1.4.1 formatR_0.10 fortunes_1.5-2 ## [9] grid_3.0.2 gtable_0.1.2 highr_0.3 iterators_1.0.6 ## [13] labeling_0.2 MASS_7.3-29 munsell_0.4.2 proto_0.3-10 ## [17] RColorBrewer_1.0-5 scales_0.2.3 tools_3.0.2
Sys.time()
## [1] "2014-05-13 15:05:40 BST"