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

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

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

  1. My_list

..

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

then i apply merge function to My_list

  1. Merge_list <- do.call(merge,My_list)

..

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

  1. make_sec <- function(){
  2. Time <- seq(as.POSIXct("2016-01-01 00:00:00"), length = 1000, by = "sec")
  3. Price <- cumsum(rnorm(1000))+100
  4. Sec <- xts(Price, order.by = Time) |>
  5. to_period(period = "minutes", k = 1) |>
  6. align.time()
  7. colnames(Sec) <- c("Open","High","Low","Close")
  8. # Sec with not all timestamps
  9. Sec[-sample(1:nrow(Sec),size = sample(3:10,1)),]
  10. }
  11. n <- 3 # can be any number
  12. My_list <- lapply(1:n, function(x) make_sec())
  13. 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.

  1. library(magrittr)
  2. library(tools)
  3. library(xts)
  4. M &lt;- do.call(&quot;merge&quot;, c(L, list(suffixes = paste(&quot;&quot;, names(L)))))
  5. L.2 &lt;- M %&gt;%
  6. fortify.zoo(melt = TRUE) %&gt;%
  7. split(file_ext(.$Series)) %&gt;%
  8. lapply(function(x) {
  9. x %&gt;%
  10. read.zoo(split = &quot;Series&quot;) %&gt;%
  11. as.xts %&gt;%
  12. na.omit %&gt;%
  13. setNames(file_path_sans_ext(names(.))) %&gt;%
  14. structure(na.action = NULL)
  15. })
  16. identical(L, L.2)
  17. ## [1] TRUE

Note

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

确定