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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:38:49+00:00 2026-06-09T11:38:49+00:00

I would like to ask for an advice regarding xts subclass. I am using

  • 0

I would like to ask for an advice regarding xts subclass. I am using xtsAttributes to add metadata information for every column of my xts numeric matrix. The metadata holds a character string with a description for each column.

So ncol(myxtsobject) = length(metadata). And I add a new class to the object as well, say myclass. Now I want to write method [.myclass extending [.xts function to also subset accordingly my metadata when subsetting xts matrix.

For example: d <- myobject[,c(2,3,23)] would produce d with 3 columns and appropriate 3 entries in metadata attributes.

Could somebody give me directions how to do it while rationally employing existing xts and matrix subsetting functions?

More details ….
There is the structure of my object below (just an minimalistic example):

# creating the object
n <- 10
ind <- Sys.time() + 1:n
col <- sin(seq(from=0, to=2*pi, length.out=n))
col2 <- cos(seq(from=0, to=2*pi, length.out=n))
d <- xts(x=cbind(col,col2), order.by=ind)
KEY1 <- paste("desc k1 -",1:ncol(d))
KEY2 <- paste("desc k2 -",1:ncol(d))
xtsAttributes(d) <- data.frame(KEY1,KEY2,stringsAsFactors=F)
d <- structure(d, class = c("dm", "xts", "zoo"))
# resulting structure
str(d)

Now, with such an object I would like to develop set of functions allowing subsetting consitent with object metadata KEY1, KEY2 so if I drop/select column 2, I will drop/select corresponding member from KEY1 and KEY2.

I am currently usig this code, which so far works. Reusing data.frame and xts subset.
These getMeta.dm(x) and is.dm(x) are my functions with obvious function.

#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#: subset.dm
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
subset.dm <- function(x,i,j,...)  {
# get my metadata, returns data.frame
md <- getMeta.dm(x)
# metadata subset
md <- md[j,]
# xts subset
myclass <- class(x)
x <- as.xts(x)
x <- x[i,j,...]
# now again assembling md object
# TODO fu() for creating dm objects
xtsAttributes(x) <- md
class(x) <- myclass
if(is.dm(x)) return(x) else stop("result is not dm object")
}

`[.dm` <- subset.dm
  • 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-09T11:38:50+00:00Added an answer on June 9, 2026 at 11:38 am

    You need to create a subsetting function for your subclass that handles the columnar metadata attributes:

    `[.dm` <- function(x, i, j, drop=FALSE, which.i=FALSE, ...) {
      # Include all args from [.xts (check by running args(xts:::`[.xts`))
      # Call the regular xts subsetting function
      res <- xts:::`[.xts`(x, i, j, drop, which.i, ...)
      cnx <- colnames(x)   # Get colnames from x
      ncn <- is.null(cnx)  # Check if there are no colnames
      if(ncn)              # If there are no colnames, add them
        colnames(x) <- sprintf("X%d",1:ncol(x))
      # Determine which columns are in the resulting object
      cols <- which(cnx %in% colnames(res))
      # Get the 'KEY' attributes from x
      xa <- xtsAttributes(x)
      # Replace the 'KEY' attributes with values from columns we keep
      xtsAttributes(res) <- list(KEY1=xa$KEY1[cols], KEY2=xa$KEY2[cols])
      if(ncn)              # Remove our colnames from res
        colnames(res) <- NULL
      res                  # return result
    }
    

    Now that we’ve defined the subclass subsetting function, let’s test it:

    > str(d[,1])
    An ‘xts’ object from 2012-08-07 16:08:47 to 2012-08-07 16:08:56 containing:
      Data: num [1:10, 1] 0 0.643 0.985 0.866 0.342 ...
     - attr(*, "dimnames")=List of 2
      ..$ : NULL
      ..$ : chr "col"
      Indexed by objects of class: [POSIXct,POSIXt] TZ: 
      xts Attributes:  
    List of 4
     $ tclass: chr [1:2] "POSIXct" "POSIXt"
     $ tzone : chr ""
     $ KEY1  : chr "desc k1 - 1"
     $ KEY2  : chr "desc k2 - 1"
    > str(d[,2])
    An ‘xts’ object from 2012-08-07 16:08:47 to 2012-08-07 16:08:56 containing:
      Data: num [1:10, 1] 1 0.766 0.174 -0.5 -0.94 ...
     - attr(*, "dimnames")=List of 2
      ..$ : NULL
      ..$ : chr "col2"
      Indexed by objects of class: [POSIXct,POSIXt] TZ: 
      xts Attributes:  
    List of 4
     $ tclass: chr [1:2] "POSIXct" "POSIXt"
     $ tzone : chr ""
     $ KEY1  : chr "desc k1 - 2"
     $ KEY2  : chr "desc k2 - 2"
    

    Looks good. Note that you can continue to use the xts-style subsetting functionality:

    > str(d["2012-08-07 16:08:50",1])
    An ‘xts’ object from 2012-08-07 16:08:50 to 2012-08-07 16:08:50 containing:
      Data: num [1, 1] 0.866
     - attr(*, "dimnames")=List of 2
      ..$ : NULL
      ..$ : chr "col"
      Indexed by objects of class: [POSIXct,POSIXt] TZ: 
      xts Attributes:  
    List of 4
     $ tclass: chr [1:2] "POSIXct" "POSIXt"
     $ tzone : chr ""
     $ KEY1  : chr "desc k1 - 1"
     $ KEY2  : chr "desc k2 - 1"
    > str(d["2012-08-07 16:08:50",2])
    An ‘xts’ object from 2012-08-07 16:08:50 to 2012-08-07 16:08:50 containing:
      Data: num [1, 1] -0.5
     - attr(*, "dimnames")=List of 2
      ..$ : NULL
      ..$ : chr "col2"
      Indexed by objects of class: [POSIXct,POSIXt] TZ: 
      xts Attributes:  
    List of 4
     $ tclass: chr [1:2] "POSIXct" "POSIXt"
     $ tzone : chr ""
     $ KEY1  : chr "desc k1 - 2"
     $ KEY2  : chr "desc k2 - 2"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask you how to add several listeners to the several
i would like to ask you for an advice. I'm currently developing application in
I would like to ask for opinions/advices regarding a a part of my algorithm.
Would like to ask for advice. there is a need for binary to have
I would like to ask you for piece of information that would help me
I am building an application using Zend_Framework and I would like to ask you
I would like to ask you for an advice about function template. I have
I would like to ask you about some advices about this code. It works,
I would like ask if there's a way to download an android layout from
What I would like ask is best illustrated by an example, so bear with

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.