-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathimportMossData.r
78 lines (56 loc) · 2.31 KB
/
importMossData.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
####################
#todo############
#check species in taxon table before inserting
#
####################
library (RODBC)
#library(gdata)
#Trim (getting rid of gdata..)
trim <-function (s, recode.factor = TRUE, ...)
{
s <- sub(pattern = "^ +", replacement = "", x = s)
s <- sub(pattern = " +$", replacement = "", x = s)
s
}
wd<-"O:\\data\\SEEDCLIM2014\\"
db<-odbcConnectAccess(paste(wd,"seedclim_2014-5-20.mdb", sep=""))
sqlTables(db)
#file.choose()
mossfilelist<-dir(path=paste(wd,"bryophytes\\",sep=""), full.names=TRUE, pattern="csv") #uncomment to loop #needs to be directory of just csv files
#for loop
import.moss<-function(filelist){
sapply(filelist,function(n){
#uncomment to loop
#n<-"O:\\data\\SEEDCLIM2014\\Seedclim 2012 sp.fix CSV\\Lavisdalen2012 sp.fix.csv" #comment to loop
print(n)
dat<-read.table(n, sep=";", header=TRUE)
dat<-dat[!is.na(dat$originPlotID),]
head(dat)
names(dat)
dat$turfID<-trim(dat$turfID)
print(max(nchar(as.character(dat$comment)))) #how long is longest comment)
#subTurfCommunity
print("mosssubturfcommunity")
subspp<-cbind(dat[,c("turfID", "year", "subPlot")],dat[, (which(names(dat)=="recorder")+1):(which(names(dat)=="BryoMeasured")) ])[dat$Measure=="Presence",]
#Find oddities in datasett:
spp0<-data.frame(turfID=NA, year=NA, subTurf=NA, bryoSpecies=NA)
spp0<-spp0[-1,]
lapply(4:ncol(subspp),function(nc){
sp<-subspp[,nc ]
spp2<-data.frame(turfID=subspp$turfID, year=subspp$year, subTurf=subspp$subPlot, bryoSpecies=names(subspp)[nc], adult=sp)
spp2<-spp2[!is.na(spp2$adult)&spp2$adult==1,] #keep only rows with presences
spp2
if(nrow(spp2)>0)spp0<<-rbind(spp0,spp2[,1:4])
invisible()
})
sqlSave(db,spp0,"bryoSubTurfCommunity", rownames=F, append=T)
}) #uncomment to loop
}
import.moss(mossfilelist)
# Codes for deleting moss:
wipemoss<-function(){
sqlQuery(db, "Delete * FROM bryoSubTurfCommunity")
print("Moss wiped. Hope you really wanted to do that!")
}
wipemoss()
close(db)