[R] unexpected behaviour of rbind with dataframe containing POSIXct

Brian Diggs diggsb at ohsu.edu
Fri Nov 12 19:37:39 CET 2010


On 11/12/2010 9:57 AM, Alexander Senger wrote:
> Hello list,
>
> here is what I stumbled upon:
>
> 1>  test<- data.frame(time=as.POSIXct((1:2),origin="2000-1-1"))
> 1>  test
>                   time
> 1 2000-01-01 00:00:01
> 2 2000-01-01 00:00:02
> 1>  rbind(test,b=1:2)
> Fehler in as.POSIXct.numeric(value) : 'origin' muss angegeben werden
>
> When I try to attach an additional row to a dataframe with a row
> containing dates I get the error shown above (which says that there was
> an error in "as.POSIXct.numeric(value)": an 'origin' has to be given).
>
> I haven't found any information on this so I would like to know whether
> this is to be regarded as a bug and be properly reported.

I do not think it is a bug; I think you may be expecting too much out of 
rbind().  rbind() [well, in this case, rbind.data.frame] will bind the 
rows of two data frame together, assuming they have the same column 
structure.  You are passing a numeric vector as the second argument, 
expecting it to be cast into a date and put into a data frame before 
binding.

The POSIXct class does not keep information about an origin used to 
convert a number into a date, so subsequent attempts to convert need the 
origin supplied (the direct error you are getting).  However, that is 
not enough; you still need to cast the vector into a data frame with the 
right column name:

rbind(test, data.frame(time=as.POSIXct(1:2, origin="2000-01-01")))
#                 time
#1 2000-01-01 00:00:01
#2 2000-01-01 00:00:02
#3 2000-01-01 00:00:01
#4 2000-01-01 00:00:02

Another though, do you really mean to add rows?  Or did you want to add 
another column?

cbind(test,b=1:2)
#                 time b
#1 2000-01-01 00:00:01 1
#2 2000-01-01 00:00:02 2



> This is my sessionInfo:
>
> 1>  sessionInfo()
> R version 2.10.1 (2009-12-14)
> i486-pc-linux-gnu
>
> locale:
>   [1] LC_CTYPE=de_DE.utf8       LC_NUMERIC=C
>   [3] LC_TIME=de_DE.utf8        LC_COLLATE=de_DE.utf8
>   [5] LC_MONETARY=C             LC_MESSAGES=de_DE.utf8
>   [7] LC_PAPER=de_DE.utf8       LC_NAME=C
>   [9] LC_ADDRESS=C              LC_TELEPHONE=C
> [11] LC_MEASUREMENT=de_DE.utf8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> loaded via a namespace (and not attached):
> [1] tools_2.10.1
>
>
> I'm aware that my version is somewhat outdated but it seems not much has
> changed with rbind() since then. Please correct me if I'm wrong.

I don't know how much has; I ran these examples with
R version 2.12.0 Patched (2010-11-04 r53526)
Platform: i386-pc-mingw32/i386 (32-bit)

> Thank you for your attention
>
> Alex

-- 
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University



More information about the R-help mailing list