1

I'm using Table() function expecting to get a contingency table but it returns 2 columns and a frequency instead of a 2 way table.

This is what I´m doing:

denue <- read.csv("DENUE 2010.csv")
str(denue)

'data.frame':   416898 obs. of  13 variables:  
$ Nombre.de.la.unidad.económica  : chr  "MINERA METALURGICA TAPALPA" "PLANTA DE TRATAMIENTO DE AGUA RECIDUAL" "DESARROLLOS INMOBILIARIOS BMU" 
$ Razón.social                   : chr  "MINERA METALURGICA TAPALPA  S A DE C V"
$ Código.de.la.clase.de.actividad: int  212221 222111 236112 236211 236211 236221
$ Nombre.de.la.clase.de.actividad: Factor w/ 900 levels "ACABADO DE PRODUCTOS
$ Personal.ocupado..estrato.     : chr  "0 A 5 PERSONAS" "6 A 10 PERSONAS" 
$ Entidad.federativa             : chr  "DISTRITO FEDERAL" "DISTRITO FEDERAL" 
$ Municipio                      : Factor w/ 16 levels "ÁLVARO OBREGÓN",..:
$ Clave                          : int  2 2 2 2 2 2 2 2 2 2 ...  
$ Localidad                      : chr  "Azcapotzalco" "Azcapotzalco"...  
$ Área.geoestadística.básica     : chr  "0699" "0932" "1023" "0487" ...  
$ Area                           : Factor w/ 2415 levels "0020010","0020025",..:
$ Latitud                        : num  19.5 19.5 19.5 19.5 19.5 ...  
$ Longitud                       : num  -99.2 -99.2 -99.2 -99.2 -99.2 ...

denue <- data.frame(table(denue$Area, denue$Nombre.de.la.clase.de.actividad))

       Var1                          Var2 Freq
1   0020010 ACABADO DE PRODUCTOS TEXTILES    0
2   0020025 ACABADO DE PRODUCTOS TEXTILES    0
3   002003A ACABADO DE PRODUCTOS TEXTILES    0
4   0020044 ACABADO DE PRODUCTOS TEXTILES    0
5   0020097 ACABADO DE PRODUCTOS TEXTILES    0
6   002010A ACABADO DE PRODUCTOS TEXTILES    0
7   0020114 ACABADO DE PRODUCTOS TEXTILES    0
8   0020129 ACABADO DE PRODUCTOS TEXTILES    0
9   0020133 ACABADO DE PRODUCTOS TEXTILES    0
10  0020148 ACABADO DE PRODUCTOS TEXTILES    0

When what I want is this:

        ACABADO DE PRODUCTOS TEXTILES . until 900th Nombre.de.la.clase.de.actividad
0020010
0020025
002003A  
0020044 

What am I doing wrong?

Thank you for your time.

René Martínez
  • 179
  • 1
  • 3
  • 11
  • 1
    You need `as.data.frame.matrix(table(denue$Area, denue$Nombre.de.la.clase.de.actividad))` – akrun Sep 20 '20 at 00:00

1 Answers1

1

If we look at the source code of as.data.frame.table

ex <- quote(data.frame(do.call("expand.grid", c(dimnames(provideDimnames(x, sep = sep, base = base)), KEEP.OUT.ATTRS = FALSE, stringsAsFactors = stringsAsFactors))

So, it expands into a two column data.frame. Instead, if we want to convert to data.frame with the similar structure, change the table to matrix and do the conversion so that it wont call as.data.frame.table instead calls as.data.frame.matrix or call as.data.frame.matrix directly (because there are several methods for as.data.frame (methods("as.data.frame"))

as.data.frame.matrix(table(denue$Area, denue$Nombre.de.la.clase.de.actividad))

Using a reproducible example with mtcars

as.data.frame.matrix(table(mtcars$vs, mtcars$am))
#   0 1
#0 12 6
#1  7 7
as.data.frame(table(mtcars$vs, mtcars$am))
#  Var1 Var2 Freq
#1    0    0   12
#2    1    0    7
#3    0    1    6
#4    1    1    7

which is same as the method

as.data.frame.table(table(mtcars$vs, mtcars$am))
#  Var1 Var2 Freq
#1    0    0   12
#2    1    0    7
#3    0    1    6
#4    1    1    7
akrun
  • 874,273
  • 37
  • 540
  • 662