Try the reshape package:
> 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> require(reshape)
Loading required package: reshape
Loading required package: plyr> str(my.df)
'data.frame': 9 obs. of 3 variables:
$ Show: Factor w/ 5 levels "Babylon 5","Dr Who",..: 1 2 2 2
3 3 4 4 5
$ Size: num 0 0.701 0.85 0.85 0.7 0.8 0.8 0.9 0.2
$ Date:Class 'Date' num [1:9] 13728 13728 13729 13730 13728
...> x <- melt(my.df, id=c("Show", "Date"))
> cast(x, Show ~ Date)
Show 2007-08-03 2007-08-04 2007-08-05
1 Babylon 5 0.000 NA NA
2 Dr Who 0.701 0.85 0.85
3 Star Trek 0.700 0.80 NA
4 Torchwood NA 0.80 0.90
5 Sarah Jane Adventures NA NA 0.20>
On Mon, Oct 5, 2009 at 7:03 AM, Tony Breyal <tony.breyal at
googlemail.com> wrote:> 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
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?