concatenating data frames with difftime columns in

2019-09-05 16:20发布

问题:

I have two data frames with exactly the same columns and I'm trying to concatenate them together using dplyr::union.

union(df.3, df.4)

However, I get the error:

Error: cannot join on columns 'run.time' x 'run.time': Can't join on > 'run.time' x 'run.time' because of incompatible types (difftime / difftime)

Being that the run.time columns are the same type, why am I getting this error?

 class(df.4$run.time)
[1] "difftime"
> class(df.3$run.time)
[1] "difftime"
> 

I know I can just use rbind to do the concatenation, but I was curious why union doesn't work.

回答1:

I feel the need to warn you about this:

I know I can just use rbind to do the concatenation

I would be very cautious about using rbind when one of your columns is a difftime. Consider this example:

now <- Sys.time()
foo <- data.frame(X = now+10 - now)
bar <- data.frame(X = now+120 - now)

foo
##          X
## 1  10 secs

bar
##          X
## 1   2 mins

rbind(foo, bar)
##          X
## 1  10 secs
## 2   2 secs   !!!

rbind(bar, foo)
##          X
## 1   2 mins
## 2  10 mins   !!!


标签: r dplyr