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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:21:34+00:00 2026-06-15T00:21:34+00:00

I have an object that I have created using the as.ts function in R,

  • 0

I have an object that I have created using the as.ts function in R, and now I would like a simple way to transform one of the variables and add it to the same ts object. So, for example

tsMloa <- ts(read.dta("http://www.stata-press.com/data/r12/mloa.dta"), frequency=12, start=1959)
tsMloa[, "meanLog"] <- tsMloa[,"log"] - mean(tsMloa[,"log"])

gives me a subscript out of bounds error. How can I get around this?

  • 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-15T00:21:35+00:00Added an answer on June 15, 2026 at 12:21 am

    Firstly, you ought to consider adding require(foreign) to your example code, as it’s necessary to run your code.

    I don’t know anything about *.dta files or their formatting, but i can tell you that if you’d like to work with time series in R, you’d do well to look into the zoo and xts family of functions.

    With that in mind, try the following:

    require(xts)
    require(foreign)
    
    tsMloa <- ts(read.dta("http://www.stata-press.com/data/r12/mloa.dta"), frequency=12, start=1959)
    
    tt <- seq(as.Date("1959-01-01"), as.Date("1990-12-01"), by='mon')
    tsMloa_x <- xts(unclass(tsMloa)[,1:3], order.by=tt)
    
    tsMloa_x$meanLog <- tsMloa_x$log - mean(tsMloa_x$log)
    

    That should do what you are looking for — and it gives you a reason to look into the very good packages.


    Doing it with zoo — plus i’ve created a function to turn your integers into months.

    require(foreign)
    require(zoo)
    
    Mloa <- read.dta("http://www.stata-press.com/data/r12/mloa.dta"), frequency=12, start=1959)
    
    intToMonth <- function(intMonth, origin = "1960-01-01"){
        dd <- as.POSIXlt(origin)
        ddVec <- rep(dd, length(intMonth))
        ddVec$mon  <- ddVec$mon + intMonth%%12
        ddVec$year <- ddVec$year + intMonth%/%12
        ddRet <- as.Date(ddVec)
        return(ddRet)
    }
    
    dateString <- intToMonth(Mloa[, 'tm'])
    
    zMloa <- zoo(Mloa[, -2], dateString)
    zMloa$meanLog <- zMloa$log - mean(zMloa$log)
    

    As i see it, your problem is with converting the timestamps in the source file to something R understands and can work with. I found this part of adapting to R especially tricky.

    The above function will take your month-integers, and turn them into a Date object. The resultant output will work with both zoo and xts as the order.by argument.

    If you need to change the origin date, just supply the second argument to the function — i.e. otherDateString <- intToMonth(timeInts, "2011-01-01").

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

Sidebar

Related Questions

So we currently have a website that was created using Django. Now, we would
I've created a object that I'd like to have accessible from wherever the request-object
I have a document that loads into an iframe. Now I would like to
I have an object, that on initialization creates a serial GCD queue using dispatch_queue_create
I have an object that hasn't been created yet (so it has no ID),
I have an ExpenseType object that I have created with the following migration: class
I have a list of class object that I created as a variable in
Good day, I have created an object that will manage data access. My app
i have created an IE band object (toolbar) that is working well. however, when
I have a stdClass object created from json_decode that won't return the right number

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.