[R-SIG-Finance] how to handle both the symbol names and prices in one xts
J Ryan
jeff.a.ryan at gmail.com
Sun Oct 11 14:33:37 CEST 2009
Wind,
This isn't possible, as an xts object is fundamentally a matrix with
an attribute ("index") for keeping track of time, and a matrix is just
a contiuous block of memory (array) with a dimension.
Building two objects using a common index would be as close as you can
get. Long term there has been some discussion and work done on having
something akin to a list of xts objects, but that isn't implemented yet.
HTH
Jeff
Jeffrey A. Ryan
jeffrey.ryan at insightalgo.com
ia: insight algorithmics
www.insightalgo.com
On Oct 11, 2009, at 4:15 AM, Wind <windspeedo99 at gmail.com> wrote:
> I wonder how to convert a dataframe with character columns and prices
> columns into xts, while maintaing the character class and numeric
> class of
> each column.
>
> Regards,
> wind
>
>>
>> dput(d1)
> list(c("sym1", "sym1", "sym1", "sym2", "sym2", "sym2"), structure(c
> (14426L,
> 14427L, 14428L, 14426L, 14427L, 14428L), class = "Date"), c(23.06,
> 24.21, 24.3, 9.57, 9.65, 9.57), c(24.19, 24.99, 24.96, 9.67,
> 9.75, 9.8), c(22.92, 24.15, 24.2, 9.54, 9.56, 9.57), c(24.14,
> 24.48, 24.93, 9.62, 9.65, 9.76), c(6787, 7974, 6116, 1370, 1527,
> 1414))
>> str(d1)
> List of 7
> $ : chr [1:6] "sym1" "sym1" "sym1" "sym2" ...
> $ :Class 'Date' int [1:6] 14426 14427 14428 14426 14427 14428
> $ : num [1:6] 23.06 24.21 24.3 9.57 9.65 ...
> $ : num [1:6] 24.19 24.99 24.96 9.67 9.75 ...
> $ : num [1:6] 22.92 24.15 24.2 9.54 9.56 ...
> $ : num [1:6] 24.14 24.48 24.93 9.62 9.65 ...
> $ : num [1:6] 6787 7974 6116 1370 1527 ...
>> d2 <- do.call(cbind,d1[c(3,4,5,6,7)])
>> str(d2)
> num [1:6, 1:5] 23.06 24.21 24.3 9.57 9.65 ...
>> d3 <- data.frame(symbol=d1[[1]], d2,stringsAsFactors=FALSE)
>> str(d3)
> 'data.frame': 6 obs. of 6 variables:
> $ symbol: chr "sym1" "sym1" "sym1" "sym2" ...
> $ X1 : num 23.06 24.21 24.3 9.57 9.65 ...
> $ X2 : num 24.19 24.99 24.96 9.67 9.75 ...
> $ X3 : num 22.92 24.15 24.2 9.54 9.56 ...
> $ X4 : num 24.14 24.48 24.93 9.62 9.65 ...
> $ X5 : num 6787 7974 6116 1370 1527 ...
>> d4 <- as.xts(d3,order.by=d1[[2]],unique=FALSE)
>> str(d4)
> An 'xts' object from 2009-07-01 to 2009-07-03 containing:
> Data: chr [1:6, 1:6] "sym1" "sym2" "sym1" "sym2" "sym1" "sym2" ...
> - attr(*, "dimnames")=List of 2
> ..$ : NULL
> ..$ : chr [1:6] "symbol" "X1" "X2" "X3" ...
> Indexed by objects of class: [Date] TZ: CST-8
> Original class: 'data.frame'
> xts Attributes:
> NULL
>>
>
>
>> sessionInfo()
> R version 2.9.2 (2009-08-24)
> i386-pc-mingw32
>
> locale:
> LC_COLLATE=Chinese (Simplified)_People's Republic of
> China.936;LC_CTYPE=Chinese (Simplified)_People's Republic of
> China.936;LC_MONETARY=Chinese (Simplified)_People's Republic of
> China.936;LC_NUMERIC=C;LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
> other attached packages:
> [1] quantmod_0.3-11 TTR_0.20-1 Defaults_1.1-1 xts_0.6-7
> [5] zoo_1.5-8
>
> loaded via a namespace (and not attached):
> [1] grid_2.9.2 lattice_0.17-25
>>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
More information about the R-SIG-Finance
mailing list