如何在“merge”转换后返回与开始时相同的对象

huangapple go评论61阅读模式
英文:

how to return the same object after "merge" transformation as it was at the beginning

问题

我现在如何将每个 xts 对象返回到 list

英文:

I have a list with an arbitrary number of elements (3 in this example)
each element is a xts object with an arbitrary number of rows

My_list

..

$Sec1
                         Open     High       Low     Close
2016-01-01 00:02:00  96.81493 103.8074  96.48305  99.31684
2016-01-01 00:03:00 100.31574 100.9088  95.27193  95.27193
2016-01-01 00:04:00  95.11235 105.5243  95.11235 105.41863
2016-01-01 00:05:00 105.85303 117.9486 104.51442 117.94861
2016-01-01 00:06:00 118.60936 121.7275 116.02890 118.76587
2016-01-01 00:07:00 118.96981 119.5623 111.49401 118.46579
2016-01-01 00:10:00 115.68374 115.6837 102.88741 108.07260
2016-01-01 00:12:00 101.51486 102.4733  97.82271  99.14294
2016-01-01 00:13:00  98.81746 103.8946  90.79351 103.89463
2016-01-01 00:15:00 100.47314 100.4731  84.78741  94.06465
2016-01-01 00:16:00  94.94257 107.0833  94.12961 102.15526
2016-01-01 00:17:00 101.74539 104.1814  92.38105  92.42347

$Sec2
                         Open      High       Low     Close
2016-01-01 00:01:00  98.37414  99.96370  90.72532  92.95577
2016-01-01 00:02:00  93.98500  93.98500  73.69129  80.18034
2016-01-01 00:04:00  76.43040  78.95213  65.05842  65.61655
2016-01-01 00:05:00  64.99275  75.81193  63.17524  74.69825
2016-01-01 00:06:00  72.37924  83.95192  72.37924  83.95192
2016-01-01 00:08:00  81.29073  83.81830  75.01105  80.61681
2016-01-01 00:12:00 106.27114 109.24152  98.20000 102.32379
2016-01-01 00:17:00 103.73324 109.78397 103.73324 107.02864

$Sec3
                         Open      High       Low     Close
2016-01-01 00:01:00  99.85170 109.91461  99.85170 107.31518
2016-01-01 00:02:00 106.66197 115.44153 104.47501 109.24811
2016-01-01 00:03:00 110.84442 118.38943 110.83901 113.95159
2016-01-01 00:04:00 114.71170 118.63914 107.73243 114.53394
2016-01-01 00:06:00 106.73027 108.71359 101.05015 101.42198
2016-01-01 00:07:00  99.97737 110.32388  98.99063 106.02683
2016-01-01 00:08:00 105.86289 114.69850 103.34898 110.09985
2016-01-01 00:09:00 110.25313 111.33632  99.71466 111.33632
2016-01-01 00:10:00 112.03636 117.08576 111.28522 111.69231
2016-01-01 00:12:00  93.97850 100.14015  83.35741  83.35741
2016-01-01 00:13:00  82.28654  89.93466  81.26308  85.61102
2016-01-01 00:15:00  82.10802  82.10802  70.98550  74.15053
2016-01-01 00:16:00  74.56891  78.30949  73.06504  77.90111

then i apply merge function to My_list

Merge_list <- do.call(merge,My_list)

..

 Merge_list
                         Open     High       Low     Close    Open.1    High.1     Low.1   Close.1
2016-01-01 00:01:00        NA       NA        NA        NA  98.37414  99.96370  90.72532  92.95577
2016-01-01 00:02:00  96.81493 103.8074  96.48305  99.31684  93.98500  93.98500  73.69129  80.18034
2016-01-01 00:03:00 100.31574 100.9088  95.27193  95.27193        NA        NA        NA        NA
2016-01-01 00:04:00  95.11235 105.5243  95.11235 105.41863  76.43040  78.95213  65.05842  65.61655
2016-01-01 00:05:00 105.85303 117.9486 104.51442 117.94861  64.99275  75.81193  63.17524  74.69825
2016-01-01 00:06:00 118.60936 121.7275 116.02890 118.76587  72.37924  83.95192  72.37924  83.95192
2016-01-01 00:07:00 118.96981 119.5623 111.49401 118.46579        NA        NA        NA        NA
2016-01-01 00:08:00        NA       NA        NA        NA  81.29073  83.81830  75.01105  80.61681
2016-01-01 00:09:00        NA       NA        NA        NA        NA        NA        NA        NA
2016-01-01 00:10:00 115.68374 115.6837 102.88741 108.07260        NA        NA        NA        NA
2016-01-01 00:12:00 101.51486 102.4733  97.82271  99.14294 106.27114 109.24152  98.20000 102.32379
2016-01-01 00:13:00  98.81746 103.8946  90.79351 103.89463        NA        NA        NA        NA
2016-01-01 00:15:00 100.47314 100.4731  84.78741  94.06465        NA        NA        NA        NA
2016-01-01 00:16:00  94.94257 107.0833  94.12961 102.15526        NA        NA        NA        NA
2016-01-01 00:17:00 101.74539 104.1814  92.38105  92.42347 103.73324 109.78397 103.73324 107.02864
                       Open.2    High.2     Low.2   Close.2
2016-01-01 00:01:00  99.85170 109.91461  99.85170 107.31518
2016-01-01 00:02:00 106.66197 115.44153 104.47501 109.24811
2016-01-01 00:03:00 110.84442 118.38943 110.83901 113.95159
2016-01-01 00:04:00 114.71170 118.63914 107.73243 114.53394
2016-01-01 00:05:00        NA        NA        NA        NA
2016-01-01 00:06:00 106.73027 108.71359 101.05015 101.42198
2016-01-01 00:07:00  99.97737 110.32388  98.99063 106.02683
2016-01-01 00:08:00 105.86289 114.69850 103.34898 110.09985
2016-01-01 00:09:00 110.25313 111.33632  99.71466 111.33632
2016-01-01 00:10:00 112.03636 117.08576 111.28522 111.69231
2016-01-01 00:12:00  93.97850 100.14015  83.35741  83.35741
2016-01-01 00:13:00  82.28654  89.93466  81.26308  85.61102
2016-01-01 00:15:00  82.10802  82.10802  70.98550  74.15053
2016-01-01 00:16:00  74.56891  78.30949  73.06504  77.90111
2016-01-01 00:17:00        NA        NA        NA        NA

The question is : how do I now return each xts object back to the list

===========
My_list creation code

make_sec <- function(){
  
Time <- seq(as.POSIXct("2016-01-01 00:00:00"), length = 1000, by = "sec")
Price <- cumsum(rnorm(1000))+100

Sec <- xts(Price, order.by = Time) |> 
           to_period(period = "minutes", k = 1) |>
           align.time()

colnames(Sec) <- c("Open","High","Low","Close")

# Sec with not all timestamps
Sec[-sample(1:nrow(Sec),size = sample(3:10,1)),]
}
n <- 3 # can be any number
My_list <- lapply(1:n, function(x) make_sec())
names(My_list) <- paste0("Sec",1:length(My_list))

答案1

得分: 0

以下是您要求的翻译内容:

"The question did not provide the input in an easily reproducible form so we will use L in the Note at the end which was created using dput."

"问题没有提供易于复现的输入形式,因此我们将使用末尾的附注中使用dput创建的L。"

"Also notice that the merge in the question loses the original list's names (Sec1, Sec2, Sec3) so to retain them suffix the column names with the corresponding list name first using merge's suffixes= argument."

"还要注意问题中的merge丢失了原始列表的名称(Sec1、Sec2、Sec3),因此为了保留它们,首先使用merge的suffixes=参数给列名添加相应的列表名称后缀。"

"Given that merged object M use fortify.zoo to convert to long form, split on the suffixes, convert to zoo and then xts, remove the NA rows and remove the suffixes we added."

"考虑到合并后的对象M,使用fortify.zoo将其转换为长格式,根据后缀拆分,然后转换为zoo,接着转换为xts,删除NA行并移除我们添加的后缀。"

"library(magrittr)"
"library(tools)"
"library(xts)"

"M <- do.call('merge', c(L, list(suffixes = paste('', names(L)))))"

"L.2 <- M %>%"
" fortify.zoo(melt = TRUE) %>%"
" split(file_ext(.$Series)) %>%"
" lapply(function(x) {"
" x %>%"
" read.zoo(split = 'Series') %>%"
" as.xts %>%"
" na.omit %>%"
" setNames(file_path_sans_ext(names(.))) %>%"
" structure(na.action = NULL)"
" })"

"identical(L, L.2)"
"## [1] TRUE"

"Note"

"L <- list(Sec1 = structure(c(96.81493, 100.31574, 95.11235, 105.85303, ..."
"(附注内容请参考原文,由于内容较长,无法一一翻译,请您参考原文内容)"

英文:

The question did not provide the input in an easily reproducible form so we will use L in the Note at the end which was created using dput.

Also notice that the merge in the question loses the original list's names (Sec1, Sec2, Sec3) so to retain them suffix the column names with the corresponding list name first using merge's suffixes= argument.

Given that merged object M use fortify.zoo to convert to long form, split on the suffixes, convert to zoo and then xts, remove the NA rows and remove the suffixes we added.

library(magrittr)
library(tools)
library(xts)

M &lt;- do.call(&quot;merge&quot;, c(L, list(suffixes = paste(&quot;&quot;, names(L)))))

L.2 &lt;- M %&gt;%
  fortify.zoo(melt = TRUE) %&gt;%
  split(file_ext(.$Series)) %&gt;%
  lapply(function(x) {
    x %&gt;%
      read.zoo(split = &quot;Series&quot;) %&gt;%
      as.xts %&gt;%
      na.omit %&gt;%
      setNames(file_path_sans_ext(names(.))) %&gt;%
      structure(na.action = NULL)
   })

identical(L, L.2)
## [1] TRUE

Note

L &lt;- list(Sec1 = structure(c(96.81493, 100.31574, 95.11235, 105.85303, 
118.60936, 118.96981, 115.68374, 101.51486, 98.81746, 100.47314, 
94.94257, 101.74539, 103.8074, 100.9088, 105.5243, 117.9486, 
121.7275, 119.5623, 115.6837, 102.4733, 103.8946, 100.4731, 107.0833, 
104.1814, 96.48305, 95.27193, 95.11235, 104.51442, 116.0289, 
111.49401, 102.88741, 97.82271, 90.79351, 84.78741, 94.12961, 
92.38105, 99.31684, 95.27193, 105.41863, 117.94861, 118.76587, 
118.46579, 108.0726, 99.14294, 103.89463, 94.06465, 102.15526, 
92.42347), dim = c(12L, 4L), dimnames = list(NULL, c(&quot;Open&quot;, 
&quot;High&quot;, &quot;Low&quot;, &quot;Close&quot;)), index = structure(c(1451624520, 1451624580, 
1451624640, 1451624700, 1451624760, 1451624820, 1451625000, 1451625120, 
1451625180, 1451625300, 1451625360, 1451625420), tzone = &quot;&quot;, tclass = c(&quot;POSIXct&quot;, 
&quot;POSIXt&quot;)), class = c(&quot;xts&quot;, &quot;zoo&quot;)), Sec2 = structure(c(98.37414, 
93.985, 76.4304, 64.99275, 72.37924, 81.29073, 106.27114, 103.73324, 
99.9637, 93.985, 78.95213, 75.81193, 83.95192, 83.8183, 109.24152, 
109.78397, 90.72532, 73.69129, 65.05842, 63.17524, 72.37924, 
75.01105, 98.2, 103.73324, 92.95577, 80.18034, 65.61655, 74.69825, 
83.95192, 80.61681, 102.32379, 107.02864), dim = c(8L, 4L), dimnames = list(
    NULL, c(&quot;Open&quot;, &quot;High&quot;, &quot;Low&quot;, &quot;Close&quot;)), index = structure(c(1451624460, 
1451624520, 1451624640, 1451624700, 1451624760, 1451624880, 1451625120, 
1451625420), tzone = &quot;&quot;, tclass = c(&quot;POSIXct&quot;, &quot;POSIXt&quot;)), class = c(&quot;xts&quot;, 
&quot;zoo&quot;)), Sec3 = structure(c(99.8517, 106.66197, 110.84442, 114.7117, 
106.73027, 99.97737, 105.86289, 110.25313, 112.03636, 93.9785, 
82.28654, 82.10802, 74.56891, 109.91461, 115.44153, 118.38943, 
118.63914, 108.71359, 110.32388, 114.6985, 111.33632, 117.08576, 
100.14015, 89.93466, 82.10802, 78.30949, 99.8517, 104.47501, 
110.83901, 107.73243, 101.05015, 98.99063, 103.34898, 99.71466, 
111.28522, 83.35741, 81.26308, 70.9855, 73.06504, 107.31518, 
109.24811, 113.95159, 114.53394, 101.42198, 106.02683, 110.09985, 
111.33632, 111.69231, 83.35741, 85.61102, 74.15053, 77.90111), dim = c(13L, 
4L), dimnames = list(NULL, c(&quot;Open&quot;, &quot;High&quot;, &quot;Low&quot;, &quot;Close&quot;)), index = structure(c(1451624460, 
1451624520, 1451624580, 1451624640, 1451624760, 1451624820, 1451624880, 
1451624940, 1451625000, 1451625120, 1451625180, 1451625300, 1451625360
), tzone = &quot;&quot;, tclass = c(&quot;POSIXct&quot;, &quot;POSIXt&quot;)), class = c(&quot;xts&quot;, 
&quot;zoo&quot;)))

huangapple
  • 本文由 发表于 2023年6月11日 20:13:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定