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 #1602] Can't merge multiple objects with character values #44

Closed
joshuaulrich opened this issue Feb 26, 2015 · 6 comments
Closed
Assignees
Labels

Comments

@joshuaulrich
Copy link
Owner

Submitted by: Ken Williams (@kenahoo)
Assigned to: Nobody
R-Forge link

It appears that the merge() function fails when fed more than 2 xts objects, and they contains character data:

> x <- xts(letters[1:5], Sys.Date() + cumsum(1:5)*60)
> merge(x)
           [,1]
2011-09-28 'a' 
2011-09-30 'b' 
2011-10-03 'c' 
2011-10-07 'd' 
2011-10-12 'e' 
> merge(x,x)
           x   x.1
2011-09-28 'a' 'a'
2011-09-30 'b' 'b'
2011-10-03 'c' 'c'
2011-10-07 'd' 'd'
2011-10-12 'e' 'e'
> merge(x, x, x)
Error in merge.xts(x, x, x) : unsupported data type

By contrast, it works fine when the data is numeric:

> merge(x)
           [,1]
2011-09-28    1
2011-09-30    2
2011-10-03    3
2011-10-07    4
2011-10-12    5
> merge(x,x)
           x x.1
2011-09-28 1   1
2011-09-30 2   2
2011-10-03 3   3
2011-10-07 4   4
2011-10-12 5   5
> merge(x,x,x)
           x x.1 x.2
2011-09-28 1   1   1
2011-09-30 2   2   2
2011-10-03 3   3   3
2011-10-07 4   4   4
2011-10-12 5   5   5

Perhaps something's being silently converted to a factor internally?

-Ken

@joshuaulrich
Copy link
Owner Author

The n-way merge case currently only works for logical, integer, and numeric types. Character, complex, and any other type will fail. It should be possible to add support for character and complex data.

@joshuaulrich joshuaulrich self-assigned this Mar 9, 2015
@SteveBronder
Copy link

SteveBronder commented Nov 3, 2016

Wanted to note this also effects lag.xts(). A workaround for that function is to do an lapply() over your lags and then bind them all together with cbind.zoo()

library(xts)
set.seed(1234)
dat = xts(data.frame(char = rep(c("A","B"),5)), order.by =as.POSIXct(paste0("1992-01-",1:10)) )

xLagDiff = lapply(1:5,function(i) {
  lags = lag.xts(dat,k = i)
  colnames(lags) = paste0(colnames(lags),i)
  lags
  })

xLagDiff2 = do.call(cbind.zoo,xLagDiff)
xLagDiff2

@joshuaulrich
Copy link
Owner Author

joshuaulrich commented Nov 3, 2016

@SteveBronder, your example doesn't run. lag is only defined as a function, so your lapply call fails. Could you please edit your example to be minimal (i.e. without requiring another package and running an ARIMA model) and reproducible (runs from a clean R session).

@SteveBronder
Copy link

@joshuaulrich apologies, amended the example

@joshuaulrich
Copy link
Owner Author

joshuaulrich commented Nov 4, 2016

@SteveBronder awesome, thanks!

Note that this only affects lag because lag calls merge when length(k) is > 2. In fact, it works if length(k) <= 2. For example:

R> lag(dat,2:3)
           char char.1
1992-01-01 NA   NA    
1992-01-02 NA   NA    
1992-01-03 "A"  NA    
1992-01-04 "B"  "A"   
1992-01-05 "A"  "B"   
1992-01-06 "B"  "A"   
1992-01-07 "A"  "B"   
1992-01-08 "B"  "A"   
1992-01-09 "A"  "B"   
1992-01-10 "B"  "A"   

@joshuaulrich
Copy link
Owner Author

Yeesh, just looked at the R-Forge issue tracker and saw:

Date Submitted: 2011-09-27 12:46

I hope "better late than never" applies... Sorry about the massive delay, @kenahoo.

@joshuaulrich joshuaulrich added this to the Release 0.11-1 milestone Aug 4, 2018
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