4

xtsサブクラスについてアドバイスをお願いします。xtsAttributesxts数値行列のすべての列のメタデータ情報を追加するために使用しています。メタデータは、各列の説明を含む文字列を保持します。

だからncol(myxtsobject) = length(metadata)。また、オブジェクトに新しいクラスを追加しますmyclass。ここで、xts行列をサブセット化するときに、それに応じてメタデータもサブセット化するメソッド[.myclass拡張関数を作成します。[.xts

例:メタデータ属性に3つの列と適切な3つのエントリを使用しd <- myobject[,c(2,3,23)]て生成します。d

誰かが既存のxtsと行列サブセット化関数を合理的に使用しながらそれを行う方法を教えてもらえますか?

詳細....以下に私のオブジェクトの構造があります(単なる最小限の例):

# 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)

ここで、このようなオブジェクトを使用して、オブジェクトメタデータKEY1、KEY2と一致するサブセット化を可能にする関数のセットを開発したいので、列2をドロップ/選択すると、KEY1とKEY2から対応するメンバーをドロップ/選択します。

私は現在このコードを使用していますが、これは今のところ機能しています。data.frameとxtsサブセットを再利用します。これらのgetMeta.dm(x)とis.dm(x)は、明らかな関数を持つ私の関数です。

#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#: 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
4

1 に答える 1

3

列メタデータ属性を処理するサブクラスのサブセット化関数を作成する必要があります。

`[.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
}

サブクラスサブセット化関数を定義したので、それをテストしてみましょう。

> 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"

いいね。xtsスタイルのサブセット化機能を引き続き使用できることに注意してください。

> 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"
于 2012-08-07T21:28:55.233 に答える