Skip to content
Prev 257953 / 398506 Next

Problem with ddply in the plyr-package: surprising output of a date-column

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
','e','e'),ID3=c("v1","v1","v1","v1","v2","v1","v1"),
The OP's data.frame contained a POSIXlt (not factor) object
in the "Date" column
  > str(df)
  'data.frame':   7 obs. of  5 variables:
   $ ID1  : chr  "1" "2" "2" "3" ...
   $ ID2  : chr  "a" "b" "b" "c" ...
   $ ID3  : Factor w/ 2 levels "v1","v2": 1 1 1 1 2 1 1
   $ Date : POSIXlt, format: "1985-05-01" "1985-05-02" ...
   $ Value: Factor w/ 7 levels "1","2","3","4",..: 1 2 3 4 5 6 7
and apparently plyr's equivalent of rbind doesn't support that class.

If you want to continue using POSIXlt objects you can get your
immediate result without ddply; subscripting will do the job:
  > nDups <- with(df, ave(rep(0,nrow(df)), ID1, ID2, ID3, FUN=length))
  > print(nDups)
  [1] 1 2 2 1 1 2 2
  > df[nDups>1, ]
    ID1 ID2 ID3       Date Value
  2   2   b  v1 1985-05-02     2
  3   2   b  v1 1985-05-03     3
  6   4   e  v1 1985-05-06     6
  7   4   e  v1 1985-05-07     7
  > str(.Last.value)
  'data.frame':   4 obs. of  5 variables:
   $ ID1  : chr  "2" "2" "4" "4"
   $ ID2  : chr  "b" "b" "e" "e"
   $ ID3  : Factor w/ 2 levels "v1","v2": 1 1 1 1
   $ Date : POSIXlt, format: "1985-05-02" "1985-05-03" ...
   $ Value: Factor w/ 7 levels "1","2","3","4",..: 2 3 6 7

If you need plyr for other tasks you ought to use a different
class for your date data (or wait until plyr can deal with
POSIXlt objects).

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com