Skip to content

Commit f7557e1

Browse files
committed
A little code cleanup
1 parent d9f8289 commit f7557e1

1 file changed

Lines changed: 50 additions & 35 deletions

File tree

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,67 @@
11
##############################################################
2-
# Miscallaneous functions that are useful in multiple contexts
2+
# Miscellaneous functions that are useful in multiple contexts
33
##############################################################
44

55
# minDiff is Minimum assumed difference between survey sets
6-
ConvertDate2SurveyDate<-function(dat,minDiff=9){
7-
dat$Date<-as.Date(dat$Date)
8-
SDates<-sort(unique(dat$Date))
9-
Dates<-data.frame(SurveyDate=SDates,Date=SDates)
10-
Dates$difDates<-c(0,diff(Dates$Date))
11-
for(i in 2:nrow(Dates)){ if(Dates$difDates[i]<minDiff){Dates$SurveyDate[i]<-Dates$SurveyDate[i-1]} }
12-
dat<-merge(dat,Dates[,-3],by='Date',all.x=TRUE)
13-
# Switch names (in order to use 'Date' subsequently)
14-
colnames(dat)[which(colnames(dat)=='Date')]<-'TrueDate'
15-
colnames(dat)[which(colnames(dat)=='SurveyDate')]<-'Date'
16-
dat<-dat[,c(length(dat),1:(length(dat)-1))]
17-
return(dat)
6+
ConvertDate2SurveyDate <- function(dat, minDiff = 9) {
7+
dat$Date <- as.Date(dat$Date)
8+
SDates <- sort(unique(dat$Date))
9+
Dates <- data.frame(SurveyDate = SDates, Date = SDates)
10+
Dates$difDates <- c(0, diff(Dates$Date))
11+
for (i in 2:nrow(Dates)) {
12+
if (Dates$difDates[i] < minDiff) {
13+
Dates$SurveyDate[i] <- Dates$SurveyDate[i - 1]
14+
}
15+
}
16+
dat <- merge(dat, Dates[, -3], by = 'Date', all.x = TRUE)
17+
# Switch names (in order to use 'Date' subsequently)
18+
colnames(dat)[which(colnames(dat) == 'Date')] <- 'TrueDate'
19+
colnames(dat)[which(colnames(dat) == 'SurveyDate')] <- 'Date'
20+
dat <- dat[, c(length(dat), 1:(length(dat) - 1))]
21+
return(dat)
1822
}
1923

2024

2125
# Match-up and rename Survey dates in two datasets
2226
# (e.g., abundance dynamics and feeding observations)
2327
# Spits out only the 2nd dataset (i.e. it conforms the dates in the 2nd dataset
2428
# to match up with those of the 1st dataset.
25-
MatchDates<-function(datA,datB,minDiff=9){
26-
datA$Date<-as.Date(datA$Date)
27-
datB$Date<-as.Date(datB$Date)
28-
SDatesA<-sort(unique(datA$Date))
29-
SDatesB<-sort(unique(datB$Date))
30-
for(i in 1:length(SDatesA)){
31-
tDate<-SDatesA[i]
32-
tDateSeq<-seq(tDate-minDiff,tDate+minDiff,1)
33-
Match<-which(datB$Date%in%tDateSeq)
34-
if(length(Match)!=0){datB$Date[Match]<-tDate}
35-
if(length(Match)==0){warning(paste0('No date match found for ',tDate))}
36-
}
37-
return(datB)
29+
MatchDates <- function(datA, datB, minDiff = 9) {
30+
datA$Date <- as.Date(datA$Date)
31+
datB$Date <- as.Date(datB$Date)
32+
SDatesA <- sort(unique(datA$Date))
33+
SDatesB <- sort(unique(datB$Date))
34+
for (i in 1:length(SDatesA)) {
35+
tDate <- SDatesA[i]
36+
tDateSeq <- seq(tDate - minDiff, tDate + minDiff, 1)
37+
Match <- which(datB$Date %in% tDateSeq)
38+
if (length(Match) != 0) {
39+
datB$Date[Match] <- tDate
40+
}
41+
if (length(Match) == 0) {
42+
warning(paste0('No date match found for ', tDate))
43+
}
44+
}
45+
return(datB)
3846
}
3947

4048
# Scale the points of a graph by their x-value
41-
ptscale<-function(x,Scale=1,base=4){return(Scale*log(x,exp(base)))}
49+
ptscale <- function(x, Scale = 1, base = 4) {
50+
return(Scale * log(x, exp(base)))
51+
}
4252

4353

44-
# Computes the variance of a weighted mean following Cochran 1977
45-
var.wtd.mean.cochran <- function(x,w,na.rm=TRUE){
46-
if(na.rm){ x<-x[!is.na(x)]; w<-w[!is.na(x)] }
47-
n = length(w)
48-
xWbar = weighted.mean(x,w)
49-
wbar = mean(w)
50-
out = n/((n-1)*sum(w)^2)*(sum((w*x-wbar*xWbar)^2)-2*xWbar* sum((w-wbar)*(w*x-wbar*xWbar))+xWbar^2*sum((w-wbar)^2))
51-
return(out)
54+
# Computes the variance of a weighted mean following Cochran 1977
55+
var.wtd.mean.cochran <- function(x, w, na.rm = TRUE) {
56+
if (na.rm) {
57+
x <- x[!is.na(x)]
58+
w <- w[!is.na(x)]
59+
}
60+
n = length(w)
61+
xWbar = weighted.mean(x, w)
62+
wbar = mean(w)
63+
out = n / ((n - 1) * sum(w) ^ 2) * (sum((w * x - wbar * xWbar) ^ 2) - 2 *
64+
xWbar * sum((w - wbar) * (w * x - wbar * xWbar)) + xWbar ^ 2 * sum((w -
65+
wbar) ^ 2))
66+
return(out)
5267
}

0 commit comments

Comments
 (0)