[R] How to get NA's into the output of xtabs?

Tony Breyal tony.breyal at googlemail.com
Mon Oct 5 13:03:17 CEST 2009


Dear all,

Lets say I have the following data frame:

> df1 <- data.frame(Show=c('Star Trek', 'Babylon 5', 'Dr Who'), Size=c(0.7, 0.0, 0.701),  Date=as.Date(c('2007-08-03', '2007-08-03', '2007-08-03'), format='%Y-%m-%d'))
> df2 <- data.frame(Show=c('Star Trek', 'Dr Who', 'Torchwood'), Size=c(0.8, 0.85, 0.8), Date=as.Date(c('2007-08-04', '2007-08-04', '2007-08-04'), format='%Y-%m-%d'))
> df3 <- data.frame(Show=c('Sarah Jane Adventures', 'Torchwood', 'Dr Who'), Size=c(0.2, 0.9, 0.85), Date=as.Date(c('2007-08-05', '2007-08-05', '2007-08-05'), format='%Y-%m-%d'))
> df.list <- list(df1, df2, df3)
> my.df <- Reduce(function(x, y) merge(x, y, all=TRUE), df.list, accumulate=F)
> my.df
                   Show  Size       Date
1             Babylon 5 0.000 2007-08-03
2                Dr Who 0.701 2007-08-03
3                Dr Who 0.850 2007-08-04
4                Dr Who 0.850 2007-08-05
5             Star Trek 0.700 2007-08-03
6             Star Trek 0.800 2007-08-04
7             Torchwood 0.800 2007-08-04
8             Torchwood 0.900 2007-08-05
9 Sarah Jane Adventures 0.200 2007-08-05
>

I would like to come up with something like this:


Show                          2007-08-03 2007-08-04 2007-08-05
Babylon 5                    0.000         NA            NA
Dr Who                       0.701         0.850         0.850
Star Trek                     0.700         0.800         NA
Torchwood                   NA            0.800         0.900
Sarah Jane Adventures NA            NA             0.200

The best i can do so far is:

> xtabs(as.numeric(Size) ~ Show + Date, data = my.df)
                       Date
Show                    2007-08-03 2007-08-04 2007-08-05
  Babylon 5                  0.000      0.000      0.000
  Dr Who                     0.701      0.850      0.850
  Star Trek                  0.700      0.800      0.000
  Torchwood                  0.000      0.800      0.900
  Sarah Jane Adventures      0.000      0.000      0.200

Many thanks in advance,
Tony


# Win Vista Ultimate
> sessionInfo()
R version 2.9.2 (2009-08-24)
i386-pc-mingw32

locale:
LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.
1252;LC_MONETARY=English_United Kingdom.
1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods
base
>




More information about the R-help mailing list