Guest User

Untitled

a guest
Jan 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #First create a vector with all my dates
  2.  
  3. #Create a frame to hold the data
  4. testdf = data.frame(sdci=rep("",1),stringsAsFactors=FALSE)
  5.  
  6. #needs library(lubridate) for month/year functions
  7. library(lubridate)
  8.  
  9. #Loop and Add data to the df
  10. for (i in 1:lenght(dates2){testdf[i, ] = c(dbGetQuery(con,paste0(' SELECT
  11. sdci_',year(dates2[i]),'_',sprintf("%02d",month(dates2[i])),'_mean from
  12. gr_sea_outlets_tier2 order by area_km2'))}
  13.  
  14. data$"2000/8/1" = dbGetQuery(db,"SELECT sdci_2000_08_mean as '2000/08/01' from gr_sea_outlets_tier2 order by area_km2 desc limit 1;")
  15.  
  16. for year in $(seq 2000 2010); do
  17. for month in $(seq -w 05 09); do
  18. data$"$year"/"$month"/01" = dbGetQuery(db,"SELECT sdci_$year_$month_mean as '$year/$month/01' from gr_sea_outlets_tier2 order by area_km2 desc limit 1;");
  19. done;
  20. done
  21.  
  22. for year in $(seq 2000 2010); do for month in $(seq -w 05 09); do echo data$'"$year"-"$month"-"01"' <- dbConnect(db,"SELECT sdci_"$year"_"$month"_mean from gr_sea_outlets_tier2 order by area_km2 desc limit 1;") >> r.cmd.data ; done; done
  23.  
  24. # Get the inputs we want
  25. years <- 2000:2010
  26. months <- sprintf("%02d", 5:9)
  27. dat <- expand.grid(months = months, years = years)
  28.  
  29. # Construct a function that gets the query given the month and year
  30. getVal <- function(month, year){
  31. query <- paste0("SELECT sdci_",
  32. year, "_", month, "_mean as '",
  33. year, "/", month,
  34. "/01' from gr_sea_outlets_tier2 order by",
  35. " area_km2 desc limit 1;")
  36. #dbGetQuery(db, query)
  37. query
  38. }
  39.  
  40. # Actually apply the function to each month/year combo of interest
  41. out <- mapply(getVal, dat$months, dat$years)
  42.  
  43. dat <- list()
  44. for(month in months){
  45. for(year in years){
  46. id <- paste(year, month, 1, sep = "/")
  47. dat[[id]] <- getVal(month, year)
  48. }
  49. }
Add Comment
Please, Sign In to add comment