Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R-Forge #2412] suffix argument in merge doesn't work #38

Closed
joshuaulrich opened this issue Feb 26, 2015 · 3 comments
Closed

[R-Forge #2412] suffix argument in merge doesn't work #38

joshuaulrich opened this issue Feb 26, 2015 · 3 comments
Assignees
Labels
Milestone

Comments

@joshuaulrich
Copy link
Owner

Submitted by: Alex Chernyakov
Assigned to: Nobody
R-Forge link

When trying to merge to xts objects, it doesn't look like the suffixes arg is working properly:

require(xts)
x <- xts(1:10, seq.Date(from=as.Date('2012-01-01'), len=10, by=1))
y <- xts(11:20, seq.Date(from=as.Date('2012-01-01'), len=10, by=1))

names(x) <- 'x'
names(y) <- 'x'

merge(x,y,suffixes=c('truex', 'truey'))

            x x.1
2012-01-01  1  11
2012-01-02  2  12
2012-01-03  3  13
2012-01-04  4  14
2012-01-05  5  15
2012-01-06  6  16
2012-01-07  7  17
2012-01-08  8  18
2012-01-09  9  19
2012-01-10 10  20
@joshuaulrich joshuaulrich self-assigned this May 9, 2022
@joshuaulrich
Copy link
Owner Author

The current solution is not consistent with merge.zoo(). The first suffix should be applied to all the columns in the first object, the second suffix to all the columns in the second object, etc.

library(xts) 
a <- data.frame(alpha=1:10, beta=2:11) 
xts1 <- xts(x=a, order.by=Sys.Date() - 1:10) 
b <- data.frame(alpha=3:12, beta=4:13) 
xts2 <- xts(x=b, order.by=Sys.Date() - 1:10) 
c <- data.frame(alpha=5:14, beta=6:15) 
xts3 <- xts(x=c, order.by=Sys.Date() - 1:10)                                               

merge(xts1, xts2, xts3, suffixes=c("A", "B", "C"))                                         
##            alpha.A beta.B alpha.C beta.A alpha.B beta.C
## 2022-05-12      10     11      12     13      14     15
## 2022-05-13       9     10      11     12      13     14
## 2022-05-14       8      9      10     11      12     13
## 2022-05-15       7      8       9     10      11     12
## 2022-05-16       6      7       8      9      10     11
## 2022-05-17       5      6       7      8       9     10
## 2022-05-18       4      5       6      7       8      9
## 2022-05-19       3      4       5      6       7      8
## 2022-05-20       2      3       4      5       6      7
## 2022-05-21       1      2       3      4       5      6

merge.zoo(xts1, xts2, xts3, suffixes=c("A", "B", "C"))                                     
           alpha.A beta.A alpha.B beta.B alpha.C beta.C
## 2022-05-12      10     11      12     13      14     15
## 2022-05-13       9     10      11     12      13     14
## 2022-05-14       8      9      10     11      12     13
## 2022-05-15       7      8       9     10      11     12
## 2022-05-16       6      7       8      9      10     11
## 2022-05-17       5      6       7      8       9     10
## 2022-05-18       4      5       6      7       8      9
## 2022-05-19       3      4       5      6       7      8
## 2022-05-20       2      3       4      5       6      7
## 2022-05-21       1      2       3      4       5      6

joshuaulrich added a commit that referenced this issue May 23, 2022
Suffixes were not in the correct order. This commit orders the suffixes
the same as merge.zoo().

See #38.
@stucash
Copy link

stucash commented Jun 30, 2022

Hi Joshua, sorry it took me some time to revert back. I've tried the fix and it exceeded my expectation (in a good way). When I was doing merge.zoo, the zoo object returned didn't really give me much information about each element's data type; xts though, has shown me just that and led me to re-evaluate my input argument, which was a xts object of mixed data type.

library(xts) 
a.int <- data.frame(alpha=1:10, beta=2:11) 
xts1.int <- xts(x=a.int, order.by=Sys.Date() - 1:10) 
b.int <- data.frame(alpha=3:12, beta=4:13) 
xts2.int <- xts(x=b.int, order.by=Sys.Date() - 1:10) 
c.int <- data.frame(alpha=5:14, beta=6:15) 
xts3.int <- xts(x=c.int, order.by=Sys.Date() - 1:10)    

a.int.str <- data.frame(alpha=1:10, beta=2:11, gamma="gamma") 
xts1.int.str <- xts(x=a.int.str, order.by=Sys.Date() - 1:10) 
b.int.str <- data.frame(alpha=3:12, beta=4:13, gamma="gamma") 
xts2.int.str <- xts(x=b.int.str, order.by=Sys.Date() - 1:10) 
c.int.str <- data.frame(alpha=5:14, beta=6:15, gamma="gamma") 
xts3.int.str <- xts(x=c.int.str, order.by=Sys.Date() - 1:10)

library(zoo)
zoo1.int <- zoo(x=a.int, order.by=Sys.Date() - 1:10) 
zoo2.int <- zoo(x=b.int, order.by=Sys.Date() - 1:10) 
zoo3.int <- zoo(x=c.int, order.by=Sys.Date() - 1:10)    

zoo1.int.str <- zoo(x=a.int.str, order.by=Sys.Date() - 1:10) 
zoo2.int.str <- zoo(x=b.int.str, order.by=Sys.Date() - 1:10) 
zoo3.int.str <- zoo(x=c.int.str, order.by=Sys.Date() - 1:10)

> merge.xts(xts1.int.str, xts2.int.str, xts3.int.str, suffixes=c("A", "B", "C")) -> xts.merged
          alpha.A beta.A gamma.A alpha.B beta.B gamma.B alpha.C beta.C gamma.C
2022-06-20 "10"    "11"   "gamma" "12"    "13"   "gamma" "14"    "15"   "gamma"
2022-06-21 " 9"    "10"   "gamma" "11"    "12"   "gamma" "13"    "14"   "gamma"
2022-06-22 " 8"    " 9"   "gamma" "10"    "11"   "gamma" "12"    "13"   "gamma"
2022-06-23 " 7"    " 8"   "gamma" " 9"    "10"   "gamma" "11"    "12"   "gamma"
2022-06-24 " 6"    " 7"   "gamma" " 8"    " 9"   "gamma" "10"    "11"   "gamma"
2022-06-25 " 5"    " 6"   "gamma" " 7"    " 8"   "gamma" " 9"    "10"   "gamma"
2022-06-26 " 4"    " 5"   "gamma" " 6"    " 7"   "gamma" " 8"    " 9"   "gamma"
2022-06-27 " 3"    " 4"   "gamma" " 5"    " 6"   "gamma" " 7"    " 8"   "gamma"
2022-06-28 " 2"    " 3"   "gamma" " 4"    " 5"   "gamma" " 6"    " 7"   "gamma"
2022-06-29 " 1"    " 2"   "gamma" " 3"    " 4"   "gamma" " 5"    " 6"   "gamma"

> merge.zoo(zoo1.int.str, zoo2.int.str, zoo3.int.str, suffixes=c("A", "B", "C")) -> zoo.merged
         alpha.A beta.A gamma.A alpha.B beta.B gamma.B alpha.C beta.C gamma.C
2022-06-21 10      11     gamma   12      13     gamma   14      15     gamma  
2022-06-22  9      10     gamma   11      12     gamma   13      14     gamma  
2022-06-23  8       9     gamma   10      11     gamma   12      13     gamma  
2022-06-24  7       8     gamma    9      10     gamma   11      12     gamma  
2022-06-25  6       7     gamma    8       9     gamma   10      11     gamma  
2022-06-26  5       6     gamma    7       8     gamma    9      10     gamma  
2022-06-27  4       5     gamma    6       7     gamma    8       9     gamma  
2022-06-28  3       4     gamma    5       6     gamma    7       8     gamma  
2022-06-29  2       3     gamma    4       5     gamma    6       7     gamma  
2022-06-30  1       2     gamma    3       4     gamma    5       6     gamma  

> class(coredata(xts.merged)[1,"alpha.A"])
# output
[1] "character"
> class(coredata(zoo.merged)[1,"alpha.A"])
# output
[1] "character"

They are both of character type however zoo had a different output format, probably.

Thanks for the swift fix.

@joshuaulrich
Copy link
Owner Author

Awesome, thanks for confirming it's fixed for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants