Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8601335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:49:05+00:00 2026-06-12T01:49:05+00:00

Solution: Combining the answer bellow: #First create a vector with all my dates #Create

  • 0

Solution:

Combining the answer bellow:

#First create a vector with all my dates

#Create a frame to hold the data
testdf = data.frame(sdci=rep("",1),stringsAsFactors=FALSE)

#needs library(lubridate) for month/year functions
library(lubridate)

#Loop and Add data to the df
for (i in 1:lenght(dates2){testdf[i, ] = c(dbGetQuery(con,paste0(' SELECT \
sdci_',year(dates2[i]),'_',sprintf("%02d",month(dates2[i])),'_mean from \ 
gr_sea_outlets_tier2 order by area_km2'))}

END OF EDIT

I am getting my data one-by-one with this command:

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;")

but since I have 50 tables the processes becomes a bit tiresome.

Im trying to create a loop for it but I can’t figure the syntax.

Here is some pseudocode (bash like) for what I want to do:

for year in $(seq 2000 2010); do
    for month in $(seq -w 05 09); do
         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;"); 
        done; 
 done

As a tempory solution ive manages to get bash to create a set of commands for me to parse into R.

 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

But Ι think its possible to do it inside R

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:49:06+00:00Added an answer on June 12, 2026 at 1:49 am

    Using a list to store the output would probably be the best option. But we’ll let mapply take care of that for us.

    # Get the inputs we want
    years <- 2000:2010
    months <- sprintf("%02d", 5:9)
    dat <- expand.grid(months = months, years = years)
    
    # Construct a function that gets the query given the month and year
    getVal <- function(month, year){
        query <- paste0("SELECT sdci_", 
                        year, "_", month, "_mean as '", 
                        year, "/", month, 
                        "/01' from gr_sea_outlets_tier2 order by",
                        " area_km2 desc limit 1;")
        #dbGetQuery(db, query)
        query
    }
    
    # Actually apply the function to each month/year combo of interest
    out <- mapply(getVal, dat$months, dat$years)
    

    I commented out the dbGetQuery call and instead just returned the query to be made. You should probably run it at least once like this to check that those are the queries you want before actually running the queries. Once you want to run them just uncomment the call to dbGetQuery and delete the line that just contains query.


    Edit: If we really want a for loop based solution then use this instead of mapply:

    dat <- list()
    for(month in months){
        for(year in years){
            id <- paste(year, month, 1, sep = "/")
            dat[[id]] <- getVal(month, year)
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Solution: API 11 is needed see answer below! Easy Question: After downloading a File
An answer at the end of this question has been filled out, combining remarks
First of all, sorry the way I write, bad English here. Please, consider the
I usually create a solution folder in Visual Studio and put my DB scripts
solution:[a-zA-Z0-9.!#$%&'*+-/=?\^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)* is a good choice I am using a regular expression like the below
SOLUTION Make sure in the plist that the storyboard name is listed as the
Solution at end of this post. By default the time is set to one
Solution: Make sure nothing is null! I have a ListView throwing a NullPointerException, and
Solution: if you have the same problem, addElement() instead of addChild() is what did
Solution: strpos turned out to be the most efficient. Can be done with substr

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.