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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:37:53+00:00 2026-06-16T22:37:53+00:00

I have a zoo class data set loaded into R and I am trying

  • 0

I have a zoo class data set loaded into R and I am trying to run a for loop. But I keep running into the error

Error in NextMethod(“[<-“) : subscript out of bounds.

The loop is pretty simple

startobs=5
for (i in startobs:(NROW(data)-startobs)) {
  data[startobs+i,"dummy"]=startobs+i

}

I think the problem is in the startobs+i. R doesn’t seem to like placing the index of the loop (i) in the data[i,], as your going through each element. What am I doing wrong?


The structure of the data is as follows

>data

     obs     date   weight  dummy
      1   2010-10-04 52495   NA 
      2   2010-10-01 53000   NA 
      3   2010-09-30 52916   NA 
      4   2010-09-29 52785   NA 
      5   2010-09-28 53348   NA 
      6   2010-09-27 52885   NA 
      7   2010-09-24 52174   NA 
      8   2010-09-23 51461   NA 
      9   2010-09-22 51286   NA 
      10  2010-09-21 50968   NA 
      11  2010-09-20 49250   NA

and

>dput(head(data))
structure(c(100, 101.01, 110.75, 111.24, 104.96, 104.95, 104.06, 
109.08, 113.48, 111.6, 108, 107.95, 95.96, 100.5, 109.05, 103.57, 
103.88, 104.66, 100.34, 108.31, 109.4, 104.87, 106, 107.91, 22351900, 
11428600, 9137200, 7631300, 4598900, 3551000, 100.34, 108.31, 
109.4, 104.87, 106, 107.91, NA, NA, NA, NA, NA, NA), .Dim = 6:7, .Dimnames = list(
    NULL, c("weight", "height", "BMI", "Age", "Age2", "Intr", 
    "Chol")), index = structure(c(12649, 12650, 12653, 12654, 
12655, 12656), class = "Date"), class = "zoo")
  • 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-16T22:37:54+00:00Added an answer on June 16, 2026 at 10:37 pm

    You mean to do:

    for (i in 1:NROW(data)) {
        data[i, "dummy"] = i
    }
    

    However, whatever you’re trying to do in a for loop will probably be easier with a vectorized solution, such as one using ifelse.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this data set made using write.zoo for which I used the following
if we have 2 class Zoo & Moo as follows: public class zoo {
I have a yearmon object: require(zoo) date1 <- as.yearmon(Mar 2012, %b %Y) class(date1) #
I have a zoo object oi.zoo with weekly data. I would like to sooth
I have an unbalanced quarterly panel data set with missing values. I want to
I have a zoo object that contains velocity data from two different points (V1
So Suppose I have a animal class managed by zoo class. Animal is an
I have a zoo with daily data that looks like this: > head(almorol) 1973-10-02
Let's say I have this class: class Zoo { protected String bearName; protected Double
I have some imported csv data that I have turned into an xts object.

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.