forked from rmbranto/FRB_MS883_MacLellanSprague
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.R
79 lines (62 loc) · 1.62 KB
/
functions.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
# R Branton Feb 2016
# normalize (i.e melt) McLelland spreadsheet tables
#install.packages("XLConnect")
library(XLConnect)
#install.packages("reshape2")
library('reshape2')
get.lz<-function(sheet.name){
# process the counts and weights tables
# read specified sheet from workbook
z<-readWorksheet(
wb,
sheet=sheet.name,
header=F)
# transpose input data table, so taxa appear as columns
z<-data.frame(
t(z),
stringsAsFactors=F
)
# get column names from row 1
colnames(z)<-
as.vector(z[1,])
# exclude first and last rows and last coulmn
nrows<-dim(z)[1]
ncols<-dim(z)[2]
z<-z[
c(-1,-1*nrows),
-1*ncols
]
# create long form table (i.e normalize) : "station" "sample" "taxa" "observation"
lz<-melt(
z,
id.vars=c(1:2),
measures.vars=(3:(dim(z)[2]-1)),
variable.name='taxa',
value.name='observation',na.rm=T)
# add sheet.name to lz
data.frame(
sheet=rep(sheet.name,dim(lz)[1]),
lz,
stringsAsFactors=F)
}
get.dates<-function(sheet.name){
# get and process the date/depth tables
# read specified sheet from workbook
z<-readWorksheet(
wb,
sheet=sheet.name,
header=F)
# transpose input data table, so taxa appear as columns
z<-data.frame(
t(z),
stringsAsFactors=F
)
# get column names from row 1
colnames(z)<-
as.vector(z[1,])
# exclude first row
z<-z[-1,]
# change
z$date<-as.Date(z$date,format='%d-%m-%Y')
data.frame(year=substr(z$date,1,4),z,stringsAsFactors=F)
}