Pergunta

Eu tenho um objeto XTS principal "Dados" com cerca de 1 milhão de linhas abrangendo 22 dias.Tenho outro objeto XTS "Set" com 22 linhas, com 1 entrada por dia.Gostaria de combinar este objeto XTS menor com o maior, de forma que tivesse uma coluna adicional contendo o valor em Set para aquele dia.

Primeiro eu tentei:

> Data=cbind(Data,as.numeric(Set[as.Date(index(Data[]))]))
Error in error(x, ...) : 
improper length of one or more arguments to merge.xts

Então eu tentei:

> Data=cbind(Data,1)
> Data[,6]=as.numeric(Set[as.Date(index(Data[,6]))])
Error in NextMethod(.Generic) : 
  number of items to replace is not a multiple of replacement length

Também tentei sem o as.numeric, mas recebi o mesmo erro.Tentei transformar Data em data.frame e recebi o erro:

Error in `[<-.data.frame`(`*tmp*`, , 6, value = c(1, 397.16, 397.115,  : 
  replacement has 22 rows, data has 835771

O que estou fazendo de errado e como faço para que isso aconteça?Só tenho usado R nas últimas duas semanas.

Obrigado!

> str(Data)
An ‘xts’ object from 2012-01-03 05:01:05 to 2012-01-31 14:59:59 containing:
  Data: num [1:835771, 1:5] 397 397 397 397 397 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "SYN" "\"WhitePack.BID_SIZE\"" "\"WhitePack.BID_PRICE\"" "\"WhitePack.ASK_PRICE\"" ...
  Indexed by objects of class: [POSIXct,POSIXt] TZ: 
  xts Attributes:  
 NULL
> str(Set)
An ‘xts’ object from 2012-01-02 to 2012-01-31 containing:
  Data: chr [1:22, 1] "  1.000" "397.160" "397.115" "397.175" "397.200" "397.390" "397.560" "397.580" "397.715" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "Settle"
  Indexed by objects of class: [POSIXct,POSIXt] TZ: 
  xts Attributes:  
 NULL
Foi útil?

Solução

Você obtém sucesso com:

df3 <- merge(Data, Set)

Para resolver minha falta de compreensão total do problema original, acho que a única etapa adicional seria:

df3[, 6]  <- na.locf( df3[, 6] )
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top