如何在R中的RollCor的输出中分割数组?

时间:2019-02-05 00:18:57

标签: r correlation data-manipulation rolling-computation

我正在尝试获得两个变量items = { item1: { cost: 100, owned: 0, name: "Item | 1", desc: "This is item 1.", img: "img/item-1.gif", }, item2: { cost: 200, owned: 0, name: "Item | 2", desc: "This is item 2.", img: "img/item-2.gif", }, item3: { cost: 300, owned: 0, name: "Item | 3", desc: "This is item 3.", img: "img/item-3.gif", } }; // Render items in their respective DIVs based on owned data $.each(items, function(key,val) { if (items[key].owned==0) { //Render name, cost and desc $('#shop').append('<div class="item"><div style="background-image:url(' + items[key].img + ')"></div><span>'+ items[key].name +'</span><span>Price: '+ items[key].cost +'</span><span>'+ items[key].desc +'</span></div>'); } else if (items[key].owned==1) { // Render name and desc $('#inventory').append('<div class="item"><div style="background-image:url(' + items[key].img + ')"></div><span>'+ items[key].name +'</span><span>'+ items[key].desc +'</span></div>'); } else if (items[key].owned==2) { // Render name $('#room').append('<div class="item"><div style="background-image:url(' + items[key].img + ')"></div><span>'+ items[key].name +'</span></div>'); } }); $('#shop').on('click', '.item', function() { //How do I set item owned to 1 here? }); $('#inventory').on('click', '.item', function() { //How do I set item owned to 2 here? }); $('#room').on('click', '.item', function() { //How do I set item owned to 1 here? }); setInterval(function(){ var save = { "owneditem1": items.item1.owned, "owneditem2": items.item2.owned, "owneditem3": items.item3.owned, }; localStorage.setItem("save",JSON.stringify(save)); }, 10000); function loadprogress() { if (localStorage.getItem("save") !== null) { var progress = JSON.parse(localStorage.getItem("save")); items.item1.owned = progress["owneditem1"]; items.item2.owned = progress["owneditem2"]; items.item3.owned = progress["owneditem3"]; }; }; DGS10之间的滚动相关性输出,但是我正在获得每一行的相关性矩阵输出。有没有一种方法可以对其进行切片以仅获取实际值[0] [1]。我是Python新手R ...

包和变量

SP500

代码

library(quantmod)
getSymbols('DGS10',src='FRED')
getSymbols('SP500',src='FRED')

输出

```{r}
merge(DGS10, SP500) %>%
  na.omit() %>%
  roll_cor(width=5)
```

所需的输出

          DGS10     SP500
DGS10 1.0000000 0.8542177
SP500 0.8542177 1.0000000

, , 6

          DGS10     SP500
DGS10 1.0000000 0.9104856
SP500 0.9104856 1.0000000

, , 7

          DGS10     SP500
DGS10 1.0000000 0.9672053
SP500 0.9672053 1.0000000

1 个答案:

答案 0 :(得分:1)

老实说,我第一次看到这样的数组-[1:2, 1:2, 1:2496]。我看到不可能通过[1, 2, 1:2496]这样的多维索引来从中获取值。它的长度为9984(2496 * 4),因此您可以使用以下方法拉取所需的值:

merge(DGS10, SP500) %>%
  na.omit() %>%
  roll_cor(width=5) %>%
  `[`(seq(2, length(.), 4)) %>%
  as.data.frame()

由于您对R是陌生的,因此对您来说似乎有点模糊,它等同于:

temp <-
  merge(DGS10, SP500) %>%
  na.omit() %>%
  roll_cor(width=5)

temp[seq(2, length(temp), 4)] %>%
  as.data.frame()

但是,我建议您使用tidyquant库。然后,您可以更清晰地获得结果:

library(tidyquant)

tq_get(c('DGS10', 'SP500'), get = 'economic.data') %>%
  spread(symbol, price) %>%
  na.omit() %>%
  tq_mutate_xy(
    x = DGS10,
    y = SP500,
    mutate_fun = runCor,
    n = 5
  )