4

I would like to make use of the compound assignment pipe operator %<>%. For example using the code below I can generate a simple data frame with the required proportions:

data("mtcars")
Vectorize(require)(package = c("magrittr", "gmodels"),
                   character.only = TRUE)

mtcars_sum <- CrossTable(mtcars$am, mtcars$cyl)$prop.row

as illustrated:

> head(mtcars_sum)
   y
x           4         6         8
  0 0.1578947 0.2105263 0.6315789
  1 0.6153846 0.2307692 0.1538462

I would like to undertake the same transformation but using the %<> pipe:

mtcars_sum %<>%
  CrossTable(mtcars$am, mtcars$cyl)$prop.row

this returns the following error:

> mtcars_sum %<>%
+   CrossTable(mtcars$am, mtcars$cyl)$prop.row
Error in .$CrossTable(mtcars$am, mtcars$cyl) : 
  3 arguments passed to '$' which requires 2

What am I missing?

G5W
  • 36,531
  • 10
  • 47
  • 80
Konrad
  • 17,740
  • 16
  • 106
  • 167
  • 2
    The `%<>%` operator works only when `mtcars_sum` already exists. It is equivalent to `mtcars_sum <- mtcars_sum %>% CrossTable(...)`. Since your `$` call has all the arguments specified and the pipe is also trying to assign `mtcars_sum` into an argument, you have one more argument than `$` can accept. – Benjamin Dec 21 '15 at 11:45
  • Thank you for the useful explanation! – Konrad Dec 21 '15 at 11:45

0 Answers0