forked from EEB590/IntroToR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
R_introduction_Aug2016.R
262 lines (146 loc) · 4.42 KB
/
R_introduction_Aug2016.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# RWorkshopDay1.R
# topics
# Using R as a calculator
# Functions
# Getting help
# Downloading new packages
# Creating data and variables
# Preparing and loading data files into R
# Getting to know your data
# Discussing your data with your computer
# Manipulating parts of data tables
#then try it yourself with a new dataset!
# ----- Using R as a calculator ------
1+2
cos(pi)
tan(1)
# Arithmetic
# + add
# - subtract
# * multiply
# / divide
# ^ exponent
# Relational
# > greater, less than
# >= greater than or equal to
# != not equal to
# == is equal to
# Logical
# ! not
# & and
# | or
# ----- Functions ------
# Have form: function(argument,argument,argument,...)
# Here, curve is the function and it can interpret
# the 2*x as the function I want to graph and
# "from" and "to" as arguments to specify x axis length
curve(2*x, from=0,to=8000, n=20)
curve(2*x^2, from=0, to=8000, n=20)
curve(2*x^3+5, from=0, to=8)
curve(cos(2*x), from=0, to=8000, n=20)
# ----- Getting help ------
# If you know a function name, you can use
# the question mark ? to open a help file
?t.test
# Help files tell possible arguments
# and give examples at the end
# Or, open help tab (at right) and type name in
# Or, google it! (There are great R forums)
# ----- Downloading new packages ------
# If the function you want is in a
# different package, use install.packages() (or use Packages tab in RStudio)
#install.packages("lme4")
# To load this so R can use it, use library() (or check box in Packages tab on RStudio)
library(lme4)
# ----- Creating data and variables ------
# Make a vector with concatenate, c()
firstfive<-c(1,2,3,4,5)
# Or save this as something
myvector <- c(1,2,3,4,5)
myvector<-c("ab","cd","ef","gh")
myvectorf<-as.factor(myvector)
# Type the name to see it
myvector
# Perform functions on vectors
mean(myvector)
#myvector2 <- myvector*2
myvector2
# Combine vectors
myvectors <- cbind(myvector, myvector2)
myvectors
# ----- Preparing and loading data files into R ------
# Determine your working directory
getwd()
# Set your working directory using setwd()
# or by using "Set as working director" in the "More"
# option in the "Files" tab on the right
setwd("~/Box Sync/Iowa State University/Teaching/Rstats/IntroToR")
# R likes .csv or .txt files
# so use Excel to save as one of those
# Read file using read.csv, naming it something
data2 <- read.csv('RWorkshopDay1.csv')
# You can ignore the wd and use file.choose()
# data <- read.csv(file.choose())
# ----- Getting to know your data ------
# What does R interpret this as? use class()
class(data)
data$Days<-as.factor(data$Days)
# Good! R interprets it as a data frame
# Look at the dimensions - rows by cols
dim(data)
# Look at the first rows with head()
head(data)
# What are the column names?
colnames(data)
# How are the rows, columns labeled?
labels(data)
# Summarize your data
summary(data)
# R describes data as numerical, factors, and integers
# Use str(data) to see what it is describing your data
str(data)
# Change class using as.factor(), as.numeric(), as.integer(), as.character()
data$Plot <- as.factor(data$Plot)
str(data)
data$Date<-as.Date(data$Date, format="%m/%d/%Y")
# ----- Discussing your data with your computer ------
# To describe cells in your data frame,
# R uses the form data[i,j]
# where i is row, j is column
# Or, data$column to describe columns
# Specific cells
data[2,5]
# Specific row
data[2,]
# Specific column
data$Veg
head(data$Veg)
head(data[,5])
# OR, data$column
data$Veg
# OR, data[['column']]
head(data[['Veg']])
# ----- Manipulating parts of data tables ------
flow <- read.table('RWorkshopDay2.txt',header=T)
# an alternative to read.csv if you can produce a text file
head(flow)
flow$init_g
str(flow)
summary(flow)
# Create a vector by calculating
# This isnt automatically attached to the "flow" data frame
dissolved <- (flow$init_g - flow$final_g)/(flow$days)
# To attach, use cbind() to add "disolved" to "flow"
flow <- cbind(flow,dissolved)
# Make sure the new column is there
head(flow)
### now it's your turn ####
# read in spider.csv
# explore dataset -what is min, max, average for the total # of webs?
# create a vector describing the number of webs per meter
# make a histogram of the total number of webs
hist()
barplot(table(spider$year,))
table(spider$year, spider$island)
with(spider, table(year,island,season))
hist(spider$tot_webs)