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

No hay comentarios:

Publicar un comentario

Nube de datos