-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIntroduction.R
178 lines (127 loc) · 3.65 KB
/
Introduction.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
######################################
## INTRODUCTION TO R SOFTWARE ##
## AGR 5266C: Field Plot Techniques ##
## FALL 2018 ##
## rramadeu at ufl dot edu ##
######################################
## Before Start
## Create a folder for your project
## Example: ~/Documents/Introduction_to_R_Class
## Open RStudio
## Create an R script (.R extension)
print("Hello World!")
## Save it
## Programming practices
## Comments!
## Working Directory
getwd()
setwd("?") ##fill with your path to archives
## RStudio: Session -> Set Working Directory -> ...
## Linux =/= Windows =/= Mac
## R AS CALCULATOR
## Sum
3+3 #I can comment or annotate my scripts by writing after the # sign
## Division
(3+3)/3
## Multiplication
3*3*3
## Potence
3^3
## Square Root
9^0.5
sqrt(9)
## Logarithm
log(10)
log(10,base = 10)
log(10,base = 2)
## Need help?
?sqrt
?log
## Function within a function
log( sqrt(9) )
log(3)
## CREATING OBJECTS
## Object classes
x = 1
y = "treatment_A"
z = 1<0
class(x)
class(y)
class(z)
## Creating vectors
dice = c( 1, 2, 3, 4, 5, 6 )
letters = c( "A", "B", "C" )
mean(dice)
sum(dice)
cumsum(dice)
sd(dice)
var(dice)
mean(letters)
## DATA INPUT & OUTPUT
## Setting working directory
setwd("~/Desktop/RLab") #Can be done by going to Session > Set Working Directory > Choose Directory
getwd() #Get working directory
## Read a file (Example 9.1 from Clewer & Scarisbrick
wheat <- read.table("Wheat_91.txt", header=T, sep=" ")
wheat
fix(wheat)
print(wheat)
head(wheat) #View top 6 lines of data
head(wheat, n=10) # View top 10 lines of data
tail(wheat)
dim(wheat) #find the dimension of data (row, column)
str(wheat) #find the structure of data
## Subsetting
## subsetting is obtained by [row,column]
## First value in leafdata, row 1, column 1
wheat[1,1]
## row 15, column 3
wheat[15,3]
# We can use the function c, which stands for combine to select more than 1 rows or columns
wheat[c(1,3), c(2,3)]
#We frequently want to select contguous rows or columns, such as the first ten rows, and columns 3 through 5. We can use c for this, but it is more convenient to use : operator. This function generates sequences of numbers
1:5
3:15
#Subset first 10 rows and last 3 columns
wheat[1:10, 2:3]
subset1 <- wheat[1:10, 2:3]
View (subset1)
#If you want to select all rows or all columns, leave that value empty
wheat[3,]
wheat[,3]
#All rows from column 2-3
wheat[,2:3]
## Addressing columns by name
#columns can be addressed by name, with either $ or square brackets wheat[,'variety']
print(wheat$variety)
wheat[,'variety']
#using two functions at a time , here we are using subset and max or min, functions can be combined
wheat[,3]
max(wheat[,3])
min(wheat[,3])
####SUMMARY####
summary(wheat)
######PLOTTING#####
plot(wheat$yield,wheat$unit)
boxplot(wheat$yield~wheat$variety)
##LOGIC OPERATIONS
index_A = which(wheat$variety=="A")
wheat_A = wheat[index_A,]
View(wheat_A)
dim(wheat_A)
str(wheat_A)
## Writing or saving data
write.csv(wheat_A, file="wheat_A.csv") #this file is saved into your working directory
getwd()
## Letting just the data with yield > 25
index_thr25 <- which(wheat$yield > 25)
wheath_thr25 <- wheat[index_thr25,]
## Analysis of Variance
model <- aov(yield~variety,data=wheat)
anova(model)
#Installing packages
#install.packages("agricolae") #this needs to be done only once
#installed package can be called by using library
library(agricolae)
hsd_output <- HSD.test(model, trt="variety", alpha=0.05)
print(hsd_output)