Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using table() function from base with dplyr pipe-syntax?

Tags:

r

dplyr

I enjoy the syntax of dplyr, but I'm struggling with easily obtaining a contingency table in the same way that I can get with the base R table() function. table() is OK, but I can't figure out how to incorporate it into the dplyr pipe syntax.

Thank you for your help.

Here is some example data that has the output I'm trying to get to.

df <- tibble(id=c(rep("A",100),rep("B",100),rep("C",100)),
               val=c(rnorm(300,mean=500,sd=100))) %>%
  mutate(val_bin=cut(val,breaks=5))

table(df$id,df$val_bin)

Output:

    (210,325] (325,440] (440,554] (554,669] (669,784]
  A         4        22        55        18         1
  B         6        19        46        24         5
  C         3        23        44        22         8
like image 671
Nickerbocker Avatar asked Feb 14 '20 22:02

Nickerbocker


1 Answers

One option is to use with:

df %>%
  with(., table(id, val_bin))
#    val_bin
# id  (228,327] (327,426] (426,525] (525,624] (624,723]
#   A         4        19        39        22        16
#   B         5        15        41        32         7
#   C         5        14        44        25        12

Technically, the . is not required,

df %>%
  with(table(id, val_bin))

but I find it is perhaps a little clearer in situations where it might be easy to confuse where the data is going (within with or table). (Hint: it's just about always the first function, with here.)

like image 90
r2evans Avatar answered Oct 19 '22 00:10

r2evans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!