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 8959279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:23:56+00:00 2026-06-15T15:23:56+00:00

I am trying to write a for-loop in r that loops over data I

  • 0

I am trying to write a for-loop in r that loops over data I have starting in 1970. I want to loop over the years themselves so that I will have an iteration for each year in the data and store the data (vote shares, incumbency status, etc) in a vector.
Obviously, since my data starts in 1970 I don’t want values for years < 1970 however, the script below is producing a vector with 0’s for in locations 1-16, NA in locations 17-1969 and then what I believe is the intended statistic in even years 1970-2000 and NA in in odd years. How do I get this to return just the values I want (even years 1970-2000?) And what is going on with the 0 values in positions 1-16?

incumb.data is data frame with the vote shares, incumbency status, election outcomes by year and state.

\script:

J<-seq(1970, 2000,2)
inc.fx1<-vector(length=length(J))
year.df1<-vector(length=length(J))

for (j in 1970:J){
  state.hold<-incumb.data[StateNumb3==26,]
  yr.hold<-state.hold[state.hold$V6==j,]

  St28.df<-data.frame(year=yr.hold$V6, 
                      v_1=yr.hold$v_1, v_2=yr.hold$v_2,
                      I1=yr.hold$I1, I2=yr.hold$I2,
                      P2=yr.hold$P2)

  St28.lm<-lm(v_2~v_1+I2+P2, data=St28.df)
  inc.fx1[j]<-coef(St28.lm)[3]
  year.df1[j]<-St28.df$year
}

\end script

So I want the inc.fx1 to hold the calculated statistic for the even years 1970-2000.Thanks for any help.

BG

  • 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-15T15:23:57+00:00Added an answer on June 15, 2026 at 3:23 pm

    J is a vector, and you can iterate over it directly.

    Replace this (which should give a warning):

    for (j in 1970:J){   # ...
    

    With this:

    for (j in J){   # ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a unit test that will loop through all action methods
I am trying to write a program in Python that will loop to keep
I am trying write a function that generates simulated data but if the simulated
Trying to write app for service technicians that will display open service calls within
Trying to write a couple of functions that will encrypt or decrypt a file
I'm trying to write a function that plots a ggplot facet_wrap plot over multiple
I'm trying to make a loop that will recurse through an array of bytes
Hey, I'm trying to write a program that will accept new tasks from people,
I am trying to write a game server that supports multiplayer over a network.
i am trying to write a competition code that will get a list of

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.