-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrices and Data frames.R
175 lines (138 loc) · 4.51 KB
/
Matrices and Data frames.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Matrices, Lists, Data frames
# 13 March 2021
# Sandra Nnadi
#-----------------------------------
# Preliminaries
library(ggplot2)
# matrix is an atomic vector wrapped up in a 2 dimensional structure
m <- matrix(data = 1:12,nrow = 4,ncol = 3)
print(m)
m <- matrix(data = 1:12,nrow = 4)
print(m)
# fill one row at a time in order
m <- matrix(data =1:12,nrow = 4,byrow =TRUE)
print(m)
# use function dim to see dimensions
dim(m)
# matrix dimensions can be changed (must be correct size)
dim(m) <- c(6,2) # six rows and two columns
print(m)
dim(m) <- c(4,3)
# get row and column numbers separately
nrow(m)
ncol(m)
# note that length of matrix is referring to atomic vector
length(m)
# add names
rownames(m) <- c("a","b","c","d")
print(m)
colnames(m) <-LETTERS[1:ncol(m)]
print(m)
rownames(m) <-letters[nrow(m):1]
print(m)
# specify single element
# print row 3, column 3
print(m[2,3])
#subset an entire row or column by leaving other dimension blank
# print row 2, showing all columns
print(m[2,])
# print column 3 showing all the rows
print(m[,3])
# print everything
print(m)
print(m[,])
print(m[])
rownames(m) <- paste("Species", LETTERS[1:nrow(m)],sep = "")
colnames(m) <- paste("Site",1:ncol(m),sep = "")
print(m)
dimnames(m) <- list(paste("Species", LETTERS[1:nrow(m)],sep = ""),paste("Site",1:ncol(m),sep = ""))
print(m)
# transpose a matrix with t() where the rows become the column and the column becomes the rows
m_transpose <- t(m)
print(m_transpose)
# add a row to a matrix with rbind()
m_transpose <- rbind(m_transpose,c(10,20,30,40))
print(m_transpose)
# fix label
rownames(m_transpose)
rownames(m_transpose)[4] <- "myfix"
print(m_transpose)
# access individual rows and columns by names
m_transpose["myfix","SpeciesA"]
# add columns to matrix with cbind
SpeciesE <- c(13,2,0,1)
m_transpose <- cbind(m_transpose,SpeciesE)
print(m_transpose)
# easy to convert back to an atomic vector
my_vec <- as.vector(m_transpose)
print(my_vec)
# lists are like an atomic vector
# the key difference is that it can hold objects of different types
my_list <- list(1:10, matrix(1:8,nrow =4,byrow =TRUE),letters[1:3],pi)
str(my_list)
print(my_list)
# using single brackets does not give the contents, it just gives the list items
my_list[4]
# if a list has 10 elements it is like a train with ten cars
# contents of the 5th car must be shown by a double bracket [[5]]
# [c[4,5,6]] creates a little train with cars 4, 5 and 6
# use double brackets then carry out the operations
my_list[[4]]-3
# once double bracket is called, individual elements can be assessed in the usual way
my_list[[2]]
my_list[[2]][4,1]
# name list items when they are created
my_list2 <- list(tester=FALSE,little_m=matrix(1:9,nrow=3))
# once we have a list that has names the named elements can be accessed with the dollar sign prefix
my_list2$little_m[2,3]
my_list2$little_m
my_list2$little_m[2,]
my_list2$little_m[2]
# the unlist function puts everything into a single atomic vector
unrolled <- unlist(my_list2)
print(unrolled)
# the common use of the list is in running a linear model or statistical model because the summary of the model is in the form of a list
# output from a linear model is a list with info we need
y_var <- runif(10)
x_var <- runif(10)
my_model <- lm(y_var~x_var) # lm is linear model, y variable is a linear function indicated by the tilde of the x variable
qplot(x=x_var,y=y_var)
# look at output from model
print(my_model)
summary(my_model)
str(summary(my_model))
# pull out what we need
summary(my_model)$coefficients
# we get a table which is a matrix of values in it
str(summary(my_model)$coefficients)
stats <- summary(my_model)$coefficients
stats["x_var","Pr(>|t|)"]
stats[2,4]
# lets get our number using unlist
u <- unlist(summary(my_model))
my_pval <- u$coefficients8
# Data frame is a specialized kind of list.
# it is a list of equal- lengthed atomic vectors
# in a matrix all of the elements are of the same data type
var_a <- 1:12 # an integer
var_b <- rep(c("Con","LowN","HighN"),each=4) # a character string
var_c <- runif(12) # a double
d_frame <- data.frame(var_a,var_b,var_c)
str(d_frame)
print(d_frame)
# add another row with rbind
# make sure it is added as a list with each named item corresponding to a column
new_data <- list(var_a=13,var_b="HighN",var_c=0.6687)
d_frame <- rbind(d_frame,new_data)
str(d_frame)
tail(d_frame)
# adding a column is simpler
var_d <- runif(13)
d_frame <- cbind(d_frame,var_d)
str(d_frame)
# more concise and elegant addition of a column
d_frame$var_e <- 13:1
str(d_frame)
head(d_frame)
d_frame$var_f <- c(rnorm(12),NA)
tail(d_frame)