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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:02:11+00:00 2026-06-18T12:02:11+00:00

I am working with several time series and their characteristic in a data.table in

  • 0

I am working with several time series and their characteristic in a data.table in
long format and I would like to construct several different types of xts
object exploiting the data.table syntax. Here is what I have in mind:

Some preliminary data

library(data.table)
set.seed(1)
DT <- data.table(
  dat = as.Date("2013-01-01") + rep(1:5, 2),
  a = c(1, -1),
  x = i <- rnorm(10),
  y = 2 * i + rnorm(10),
  z = 3 * i + rnorm(10))

Lets (try to) construct 3 different time series

DT[a == 1,{
  x.ts <- xts(x, order.by = dat)
  y.ts <- xts(y, order.by = dat)
  }]
DT[
  , list(sz = mead(z/x)), by = dat][   # operate on columns of the datatable
  , z.ts <- xts(sz, order.by = dat)]    # construct xts

This code has two problems:

  1. it prints to screen y.ts and z.ts;
  2. the objects are created only in the j environment not in the global
    environment.

To solve (2), one can name the objects in the global enviroment and use <<-:

x.ts <- y.ts <- z.ts <- NA
DT[a == 1,{
  x.ts <<- xts(x, order.by = dat)
  y.ts <<- xts(y, order.by = dat)
  }
]
DT[
  , list(sz = mean(z/x)), by = dat][
  , z.ts <<- xts(sz, order.by = dat)]

And indeed

> z.ts
               [,1]
2013-01-02 2.300730
2013-01-03 4.969685
2013-01-04 1.959377
2013-01-05 1.961270
2013-01-06 3.256254

How can I make this type of assignment within a data table silent?
Is this use of <<- justified or there is a better way of doing 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-18T12:02:24+00:00Added an answer on June 18, 2026 at 12:02 pm

    <<- is justifiable, if somewhat opaque.

    Whatever you do in j, unless you are using :=, if you don’t assign the result of DT[] to something, you end up printing the result of what `DT[] evaluates to (the result of the j argument in these cases y.ts and z.ts and respectively.

    Just wrap these calls in invisible, and it won’t print, or make the outcome something else

    Using invisible

    # 
    x.ts <- y.ts <- z.ts <- NA
    invisible(DT[a == 1,{
      x.ts <<- xts(x, order.by = dat)
      y.ts <<- xts(y, order.by = dat)
      }
    ])
    invisible(DT[
      , list(sz = mean(z/x)), by = dat][
      , z.ts <<- xts(sz, order.by = dat)])
    

    Evaulating j to something else (assigning along the way)

    x.ts <- y.ts <- z.ts <- NA
    DT[a == 1,{
      x.ts <<- xts(x, order.by = dat)
      y.ts <<- xts(y, order.by = dat)
      'Assigned y.ts and x.ts to global environment)'
      }
    ]
    DT[
      , list(sz = mean(z/x)), by = dat][
      , {z.ts <<- xts(sz, order.by = dat)
          'assigned x.ts'}]
    

    In which case the results will be
    'Assigned y.ts and x.ts to global environment)' and 'assigned x.ts' respectively.

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

Sidebar

Related Questions

I am working on time series data, for which the key column is a
I'm having a time series data sets comprising of 10 Hz data over several
I'm working on several Jython projects using libraries written in Java. I'd like to
I am working on several reports all of which have similar layouts I would
I'm working on several projects, each of which uses different indentation style (for various
My application has several workers (working on different things as different processes) and some
I'm working on a one-time PHP (5.2.6) script that migrates several million of MySQL
I am working on a project which involves using several standard algorithms, like Quick
I was happily working in C++, until compilation time arrived. I've got several classes
i working on a project which compare between load time of websites on different

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.