Advertisement
Guest User

Untitled

a guest
May 25th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. library(reshape2) #restructures data with melt(),dcast()
  2. library(nrsa)
  3. library(plyr) # data management functions
  4. #library(dplyr) # even better dataframe manipulation functions
  5. library(RODBC) # connects to databases
  6. #library(magrittr) # piping package
  7. #library(rChoiceDialogs) # enables GUI working directory selection
  8.  
  9. # Restrict number of decimal places reported in output
  10. options(digits=3)
  11.  
  12. # Set your working directory, calculator will not run without correct file path
  13. #setwd(choose.dir(caption='Select Working Directory'))
  14.  
  15. # Connect to Access Database and read in Access tables
  16. channel <- odbcConnectAccess("PHab_03_25_2016.mdb")
  17. reach <- sqlQuery(channel, "SELECT * FROM Reach ORDER BY SampleID")
  18. thalweg <- sqlQuery(channel, "SELECT * FROM Thalweg ORDER BY SampleID")
  19. wet <- sqlQuery(channel, "SELECT * FROM Wet ORDER BY SampleID")
  20. bankfull <- sqlQuery(channel, "SELECT * FROM BankFull ORDER BY SampleID")
  21. wood <- sqlQuery(channel, "SELECT * FROM Wood ORDER BY SampleID")
  22. incision <- sqlQuery(channel, "SELECT * FROM Incision ORDER BY SampleID")
  23. embed <- sqlQuery(channel, "SELECT * FROM Embeddedness ORDER BY SampleID")
  24. substrate <- sqlQuery(channel, "SELECT * FROM Substrate ORDER BY SampleID")
  25.  
  26. kVisitIdVars <- c('SampleID','StationID','Date')
  27.  
  28. ## Residual Pool Calculations
  29. # Combine columns from the Reach and Thalweg tables
  30. kThalwegMeasures <- c('ThalwegN','ReachLength','Interval','Slope')
  31. thalweg <- join(thalweg,
  32. reach[, c(kVisitIdVars, kThalwegMeasures)],
  33. by = kVisitIdVars)
  34. is.na(thalweg)[,grep('1[0-4]', names(thalweg))] <- thalweg$ThalwegN == 10
  35.  
  36. mthal <- melt(thalweg,
  37. id.vars = c(kVisitIdVars, kThalwegMeasures),
  38. na.rm = T)
  39. depth.met <- calculateThalwegDepthMetrics(uid = mthal$SampleID,
  40. is.wadeable = rep(TRUE, nrow(mthal)),
  41. depth = mthal$value)
  42.  
  43. # Substrate
  44. lsub <- melt(substrate,
  45. id.vars = kVisitIdVars,
  46. variable.name = 'Location',
  47. value.name = 'SubstrateType')
  48.  
  49. is.na(lsub$SubstrateType) <- lsub$SubstrateType == 'MISSING'
  50. sub.met <- calculateWadeSubstrateMetrics(uid = lsub$SampleID,
  51. size.class = lsub$SubstrateType)
  52.  
  53. # Inside Outside calcs
  54. lsub$inout <- grepl('Left Middle|Middle|Right Middle', lsub$Location)
  55. f <- function(x){
  56. calculateWadeSubstrateMetrics(x$SampleID, x$SubstrateType)
  57. }
  58. sub.met.inout <- ddply(lsub, .(inout), f)
  59.  
  60. # Embededness
  61. membed <- melt(embed, id.vars = kVisitIdVars)
  62. embed.met <- calculateSubstrateEmbed(uid = membed$SampleID,
  63. embed = membed$value,
  64. is.center = grepl('M', membed$variable))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement