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

  • Home
  • SEARCH
  • 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 8330999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:17:31+00:00 2026-06-09T02:17:31+00:00

How can you add variables to a dataframe in a for-loop? I would like

  • 0

How can you add variables to a dataframe in a for-loop?

I would like to create a dataframe where each column is the revenue for a region between 2009 and 2011.

regions = c('A','APAC','CEE','LATAM','ME', 'NA', 'WE')

# Loop through all regions, and add them as a column in my dataframe.
for (region in regions) {

  # create the query string
  query_string  = sprintf("SELECT date, revenue
                  FROM country_revenue
                  WHERE region = '%s'
                  AND date>='2009-01-01'
                  AND date<='2011-12-31'
                  ORDER BY date ASC
                  LIMIT 2000", region)

  # Query the database, and assign the result to a variable.
  assign(sprintf('rev.%s',region), mysql_query(query_string))

  # I only want the 2nd column returned from my query above. 
  # THIS IS THE PART THAT FAILS. Error in sprintf("rev.%s", region)[, 2] : incorrect number of dimensions
  sprintf('rev.%s',region) = sprintf('rev.%s',region)[,2]

  # Add this variable to my data frame.
  revenue = cbind(revenue, sprintf('rev.%s',region))
}
  • 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-09T02:17:32+00:00Added an answer on June 9, 2026 at 2:17 am

    That would be pretty inefficient. Why not return region as part of the SQL call so you have something like

    foo <- data.frame(date = rep(Sys.Date() + 0:4, 7),
                      revenue = runif(7*5),
                      region = rep(c('A','APAC','CEE','LATAM','ME', 'NA', 'WE'), 
                                   each = 5))
    
    > head(foo)
            date   revenue region
    1 2012-08-04 0.1170867      A
    2 2012-08-05 0.6173779      A
    3 2012-08-06 0.9860934      A
    4 2012-08-07 0.1344043      A
    5 2012-08-08 0.5570391      A
    6 2012-08-04 0.5844136   APAC
    

    It is a simple dcast() call to reshape the data into the desired format.

    > require(reshape2)
    > dcast(foo, date ~ region, value.var = "revenue")
            date         A      APAC       CEE      LATAM         ME
    1 2012-08-04 0.1170867 0.5844136 0.8011066 0.82864796 0.85856770
    2 2012-08-05 0.6173779 0.7893151 0.3991653 0.41268349 0.05925445
    3 2012-08-06 0.9860934 0.2812308 0.2272009 0.04599903 0.82367709
    4 2012-08-07 0.1344043 0.7513777 0.8022602 0.96933913 0.61501816
    5 2012-08-08 0.5570391 0.2915478 0.4601065 0.82996462 0.83779233
             NA         WE
    1 0.4833374 0.25713295
    2 0.9574843 0.22122544
    3 0.5575645 0.03492411
    4 0.2962364 0.51973593
    5 0.9020639 0.95506837
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how I can add the following code to my
How can I add extra flag variables (like CPPFLAGS) that will apply to all
Where Linux/Unix environment variables are kept? How can I add my own environment variable
I would like to add syntax to Jython to enable a nicer API for
I have a data frame in R, and I'd like to add dummy variables
what I would like to do is to add several sequence numbers to one
how i can add this variables to the code above? i need to pass
I'm an R/ggplot newbie. I would like to create a geom_line plot of a
How can I add variables into my selectors : function addCurrentClass(){ var loc =
How can I add variables and quotes to a variable? in the output it

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.