Full_Name: Daniel Frey
Version: 1.7.1
OS: Windows 2000
Submission from: (NULL) (80.254.164.242)
Generating a data frame out of a xtabs result acts unusual. Take the following
sample to reproduce it:
> a.a <-
c("a","a","a","b","b")
> a.b <-
c("c","c","d","e","f")
> a.df <- data.frame(list("A"=a.a,"B"=a.b))
> a.x <- xtabs(~A+B,a.df)
> a.x
B
A c d e f
a 2 1 0 0
b 0 0 1 1
> data.frame(a.x)
A B Freq
1 a c 2
2 b c 0
3 a d 1
4 b d 0
5 a e 0
6 b e 1
7 a f 0
8 b f 1
I would expect something like a.x itself. Instead I have to give the exact
bounds, otherwise it doesn't convert properly:
> data.frame(a.x[1:2,1:4])
c d e f
a 2 1 0 0
b 0 0 1 1
I consider this a bug, as consistency of the handling breaks here.
Daniel Frey
daniel.frey@switzerland.org wrote:> > Full_Name: Daniel Frey > Version: 1.7.1 > OS: Windows 2000 > Submission from: (NULL) (80.254.164.242) > > Generating a data frame out of a xtabs result acts unusual. Take the following > sample to reproduce it: > > > a.a <- c("a","a","a","b","b") > > > a.b <- c("c","c","d","e","f") > > > a.df <- data.frame(list("A"=a.a,"B"=a.b)) > > > a.x <- xtabs(~A+B,a.df) > > > a.x > B > A c d e f > a 2 1 0 0 > b 0 0 1 1 > > > data.frame(a.x) > A B Freq > 1 a c 2 > 2 b c 0 > 3 a d 1 > 4 b d 0 > 5 a e 0 > 6 b e 1 > 7 a f 0 > 8 b f 1 > > I would expect something like a.x itself. Instead I have to give the exact > bounds, otherwise it doesn't convert properly: > > > data.frame(a.x[1:2,1:4]) > c d e f > a 2 1 0 0 > b 0 0 1 1 > > I consider this a bug, as consistency of the handling breaks here. > > Daniel Freya) data.frame() calls as.data.frame() b) as.data.frame() uses its method as.data.frame.table(), because class(a.x) [1] "xtabs" "table" c) ?as.data.frame.table tells us: `as.data.frame.table' is a method for the generic function `as.data.frame' to convert the array-based representation of a contingency table to a data frame containing the classifying factors and the corresponding counts (the latter as component `Freq'). This is the inverse of `xtabs'. So, what is the bug? I guess you expect something like data.frame(unclass(a.x)) Uwe Ligges
>>>>> daniel frey writes:> Full_Name: Daniel Frey > Version: 1.7.1 > OS: Windows 2000 > Submission from: (NULL) (80.254.164.242)> Generating a data frame out of a xtabs result acts unusual. Take the following > sample to reproduce it:>> a.a <- c("a","a","a","b","b")>> a.b <- c("c","c","d","e","f")>> a.df <- data.frame(list("A"=a.a,"B"=a.b))>> a.x <- xtabs(~A+B,a.df)>> a.x > B > A c d e f > a 2 1 0 0 > b 0 0 1 1>> data.frame(a.x) > A B Freq > 1 a c 2 > 2 b c 0 > 3 a d 1 > 4 b d 0 > 5 a e 0 > 6 b e 1 > 7 a f 0 > 8 b f 1> I would expect something like a.x itself. Instead I have to give the exact > bounds, otherwise it doesn't convert properly:>> data.frame(a.x[1:2,1:4]) > c d e f > a 2 1 0 0 > b 0 0 1 1> I consider this a bug, as consistency of the handling breaks here.See ?table: The 'as.data.frame' method for objects inheriting from class '"table"' can be used to convert the array-based representation of a contingency table to a data frame containing the classifying factors and the corresponding counts (the latter as component 'Freq'). This is the inverse of 'xtabs'. and R> class(a.x) [1] "xtabs" "table" in your example. Use data.frame(I(x)) to prevent conversion to the data frame representation of the contingency table. -k