MichaelChirico / r-bugs

A ⚠️read-only⚠️mirror of https://bugs.r-project.org/
20 stars 0 forks source link

[BUGZILLA #3754] xtabs to dataframe fails #1842

Closed MichaelChirico closed 4 years ago

MichaelChirico commented 4 years ago

From: daniel.frey@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::> 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


METADATA

MichaelChirico commented 4 years ago

From: Kurt Hornik <hornik@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>>

>>>> 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


METADATA

MichaelChirico commented 4 years ago

From: Uwe Ligges <ligges@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>-dortmund.de>

daniel.frey@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::> 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 Frey

a) 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 ofxtabs'.

So, what is the bug?

I guess you expect something like data.frame(unclass(a.x))

Uwe Ligges


METADATA

MichaelChirico commented 4 years ago

Audit (from Jitterbug): Thu Aug 21 17:55:00 2003 thomas moved from incoming to feature&FAQ


METADATA