Pipe operator (%>%) does not work

This is a recurring problem for me - pipe operator from dplyr package doesn't work in many cases.
I'm sharing a screen recording.

What might be the problem?

I don't think pipe is the problem here. It's that cor() are not designed for the pipe use case, the simple cor(examData$Anxiety, examData$Exam) should work. Or if you really prefer the pipe, try

examData %>% 
  filter(Gender == "Male") %>% 
  with(cor(Anxiety, Exam))

Thank you for answering my question. "with" works, although I don't understand why pipe operator works only in some cases but not in others.

By default, %>% passes the LHS as the 1st argument to the function in RHS. For cor, that is not the correct input. And since it does not have a data argument, you can't use it, unless you use with or within.

In this situation, use %$% operator, also from magrittr. From the docs:

Expose the names in lhs to the rhs expression. This is useful when functions do not have a built-in data argument.

An example:


mtcars %>%
    filter(gear == 4) %$%
    cor(x = mpg,
        y = disp)

Thank you! This is very helpful.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.