Mostrando entradas con la etiqueta table. Mostrar todas las entradas
Mostrando entradas con la etiqueta table. Mostrar todas las entradas

2019-10-18

How to subset a contingency table in R?

Problem

We'd like to subset a contingency table. In our example, we use the dataset chickwts, subsetting those types of feed for which we have more than 11 observations.

table(chickwts$feed)
   casein horsebean   linseed  meatmeal   soybean sunflower 
       12        10        12        11        14        12

Solution

  • Base package
  • Using the function subset.

    subset(data.frame(table(chickwts$feed)), Freq > 11)
    
  • dplyr
  • library(dplyr)
    chickwts %>% 
      count(feed) %>%
      filter(n > 11) 
    
    

Results

 # base
      Var1 Freq
1    casein   12
3   linseed   12
5   soybean   14
6 sunflower   12

# dplyr

# A tibble: 4 × 2
       feed     n
      
1    casein    12
2   linseed    12
3   soybean    14
4 sunflower    12

References

2019-07-03

How to calculate the percent of column total in R

Problem

We want to calculate the percent of column total in R. In our example, the percent of column freq: 7/397, 23/397, etc.

    x freq
1 Jan    7
2 Feb   23
3 Mar   86
4 Apr  281
Data:

df <- read.table(text = "x    freq
                        Jan   7
                        Feb   23
                        Mar   86
                        Apr   281", 
                        header = TRUE)

Solution

We create the percent of column total using the function prop.table.

df$prob <- prop.table(df$freq)
# Percentages with two decimal places
df$prob <- round(prop.table(df$freq), 4)*100
    x freq  prob
1 Jan    7  1.76
2 Feb   23  5.79
3 Mar   86 21.66
4 Apr  281 70.78
If we'd like to calculate the percent of a specific row, February in our example:
prop.table(df$freq)[df$x == "Feb"] 
 [1] 0.05793451

Alternatives

  • Base package
  • df$prob <- df$freq/sum(df$freq)
    
  • dplyr
  • library(dplyr)
    df %>% mutate(prob = prop.table(freq))
    # Or
    df %>% mutate(prob = freq / sum(freq))
    
    A specific row:
    df %>% filter(x == "Feb")
    
        x freq       prob
    1 Feb   23 0.05793451
    

References

2019-06-29

Transforming contingency tables into frequency tables in R

Problem

We want to tranform a contingency table into a frequency table in R.

# Contingency table
tbl <- table (mtcars[, c("am", "gear")])
   gear
am   3  4  5
  0 15  4  0
  1  0  8  5

Frequency tables

We transform the contingency table into a data frame.

df <- as.data.frame(tbl)
df
  am gear Freq
1  0    3   15
2  1    3    0
3  0    4    4
4  1    4    8
5  0    5    0
6  1    5    5

Transforming frequency tables in contingency tables

To transform a frequency table back to a contingency table.

ftable(xtabs(Freq ~ am + gear, data = df)) 
   gear  3  4  5
am              
0       15  4  0
1        0  8  5
It is the equivalent of:

ftable(mtcars[, c("am", "gear")])

References

2019-06-28

Proportion tables in R

Problem

We want to create proportion tables for one or multiple variables.

Solution

  • One variable
  • tabla <- table(mtcars$am)
    prop.table(tabla)
    
          0       1 
    0.59375 0.40625
    
  • Two variables
  • tabla <- table(mtcars[, c("am", "gear")])
    prop.table(tabla)
    
       gear
    am        3       4       5
      0 0.46875 0.12500 0.00000
      1 0.00000 0.25000 0.15625
    
    The prop.table function has two arguments:

    • x, table created with the function table
    • margin, with three possible values:
    •   Null - x/sum(x) default like in the previous example.
        1 - proportion calculated by rows.
        2 - proportion calculated by columns.

    # By row
    prop.table(tabla, 1)
    
       gear
    am          3         4         5
      0 0.7894737 0.2105263 0.0000000
      1 0.0000000 0.6153846 0.3846154
    
    # By column
    prop.table(tabla, 2)
    
       gear
    am          3         4         5
      0 1.0000000 0.3333333 0.0000000
      1 0.0000000 0.6666667 1.0000000
    
  • Three variables
  • tabla <- table(mtcars[, c("am", "gear", "cyl")])
    prop.table(tabla)
    
    , , cyl = 4
    
       gear
    am        3       4       5
      0 0.03125 0.06250 0.00000
      1 0.00000 0.18750 0.06250
    
    , , cyl = 6
    
       gear
    am        3       4       5
      0 0.06250 0.06250 0.00000
      1 0.00000 0.06250 0.03125
    
    , , cyl = 8
    
       gear
    am        3       4       5
      0 0.37500 0.00000 0.00000
      1 0.00000 0.00000 0.06250
    
  • Flat Contingency Table
  • In the previous example, a better approach would be to create a flat contingency table..

    tabla <- ftable(mtcars[, c("am", "gear", "cyl")])
    prop.table(tabla)
    
            cyl       4       6       8
    am gear                            
    0  3        0.03125 0.06250 0.37500
       4        0.06250 0.06250 0.00000
       5        0.00000 0.00000 0.00000
    1  3        0.00000 0.00000 0.00000
       4        0.18750 0.06250 0.00000
       5        0.06250 0.03125 0.06250
    
  • Percentage table
  • We can use the function round.

    round(prop.table(tabla)*100, 2)
    
             cyl     4     6     8
    am gear                      
    0  3         3.12  6.25 37.50
       4         6.25  6.25  0.00
       5         0.00  0.00  0.00
    1  3         0.00  0.00  0.00
       4        18.75  6.25  0.00
       5         6.25  3.12  6.25
    
    round(prop.table(tabla, 1)*100, 2) # By row, am y gear.
    
            cyl     4     6     8
    am gear                      
    0  3         6.67 13.33 80.00
       4        50.00 50.00  0.00
       5          NaN   NaN   NaN
    1  3          NaN   NaN   NaN
       4        75.00 25.00  0.00
       5        40.00 20.00 40.00
    
    round(prop.table(tabla, 2)*100, 2) # By column, cyl
    
            cyl     4     6     8
    am gear                      
    0  3         9.09 28.57 85.71
       4        18.18 28.57  0.00
       5         0.00  0.00  0.00
    1  3         0.00  0.00  0.00
       4        54.55 28.57  0.00
       5        18.18 14.29 14.29
    

References

2019-06-21

Contingency tables in R

Problem

We want to create a contingency table for one or multiple variables.

Solution

  • One variable
  • table(mtcars$am)
    
     0  1 
    19 13 
    
  • Two variables
  • table(mtcars$am, mtcars$gear)
    
         3  4  5
      0 15  4  0
      1  0  8  5
    
    If we want to include the names of the variables:

    table(mtcars[, c("am", "gear")]) 
    tabla <- table(mtcars[, 9:10])
    # or the argument dnn:
    table(mtcars$am, mtcars$gear, dnn = c("am", "gear"))
    
       gear
    am   3  4  5
      0 15  4  0
      1  0  8  5
    
  • Three variables
  • table(mtcars[, c("am", "gear", "cyl")])
    
    , , cyl = 4
    
       gear
    am   3  4  5
      0  1  2  0
      1  0  6  2
    
    , , cyl = 6
    
       gear
    am   3  4  5
      0  2  2  0
      1  0  2  1
    
    , , cyl = 8
    
       gear
    am   3  4  5
      0 12  0  0
      1  0  0  2
    
  • Flat contingency tables
  • In the previous example, a better approach would be to create a flat contingency table.

    ftable(mtcars[, c("am", "gear", "cyl")])
    
            cyl  4  6  8
    am gear             
    0  3         1  2 12
       4         2  2  0
       5         0  0  0
    1  3         0  0  0
       4         6  2  0
       5         2  1  2
    
    We use the arguments row.vars and col.vars to provide the numbers or names of the variables to be used for the rows and columns of the flat contingency table. If neither of these two is given, the last variable is used for the columns. In our example the variable cyl.

    ftable(mtcars[, c("am", "gear", "cyl")], col.vars = c(1, 2))
    
         am    0        1      
        gear  3  4  5  3  4  5
    cyl                       
    4         1  2  0  0  6  2
    6         2  2  0  0  2  1
    8        12  0  0  0  0  2
    

Alternative

The function xtabs creates contingency tables using a formula interface, each variable separated by +.

# One variable
xtabs(~ am, mtcars)
# Two variables
xtabs(~ am + gear, mtcars)
# Three variables
xtabs(~ am + gear + cyl, mtcars)
# Flat contingency table
ftable(xtabs(~ am + gear + cyl, mtcars))

Related posts

2019-05-03

Drop unused levels from a factor in R

Problem

If we filter a data frame containing a factor and then perform any operation, such as creating a contingency table, R will still show the unused levels. Subsetting does not in general drop unused levels.

df <- data.frame(name = c("a", "a", "a", "b", "b", "c", "c", "c", "c"), x = 1:9)
library(dplyr)
aa <-  df %>%
  group_by(name) %>%
  filter(n() < 4) %>% 
  droplevels()
table(aa$name)
In our example, the level c is still included in the results. We'd like to remove it and display only the used levels a and b.

# Resultado
a b c 
3 2 0
# Resultado deseado
a b 
3 2

Solution

There are two alternatives, the function droplevels or factor.

table(droplevels(aa$name))
table(factor(aa$name))
If we are using dplyr and the pipe operator:

aa <-  df %>%
  group_by(name) %>%
  filter(n() < 4) %>% 
  droplevels()
table(aa$name)

# Better still
df %>%
  group_by(name) %>%
  filter(n() < 4) %>% 
  droplevels() %>% 
  {table(.$name)}

Related posts

References

2017-08-31

Eliminar los niveles no usados de un factor en R

Problema

Cuando filtramos un data frame que contiene un factor, y creamos por ejemplo una tabla de contingencia, R nos muestra también aquellos niveles del factor no usados.

df <- data.frame(name = c("a", "a", "a", "b", "b", "c", "c", "c", "c"), x = 1:9)
library(dplyr)
aa <-  df %>%
  group_by(name) %>%
  filter(n() < 4) %>% 
  droplevels()
table(aa$name)
En este ejemplo muestra c, cuando queremos que muestre solamente a y b.

# Resultado
a b c 
3 2 0
# Resultado deseado
a b 
3 2

Solución

Usamos la función droplevels o factor.

table(droplevels(aa$name))
table(factor(aa$name))
O como estamos empleando el paquete dplyr.

aa <-  df %>%
  group_by(name) %>%
  filter(n() < 4) %>% 
  droplevels()
table(aa$name)

# Mejor aún:
df %>%
  group_by(name) %>%
  filter(n() < 4) %>% 
  droplevels() %>% 
  {table(.$name)}

Entradas relacionadas

Referencias

Nube de datos