Search code examples
rdplyrmagrittr

Can someone help me give a function to %>%


I am trying to get %>% to resemble a function like this

phase<-csvdata%>%
    filter(csvdata$phase=="test")

or like this

mergedrows1<-phase%>%
    group_by(phase$subject,phase$phase)

where I can group specific variables together from a data frame.


Solution

  • The idea of the %>% pipe is that the first argument (typically reserved to be for the name of the data frame you are manipulating in most pipe-friendly functions) is moved ahead of the pipe. In other words, f(x, y) is equivalent to x %>% f(y). In plain English, think of it as "take this data and then apply this function".

    The problem with your code is that you are calling a variable within a data frame twice, which leaves the dplyr functions like filter() and group_by() confused. You are calling the data frame once when you say csvdata %>% and again when you say csvdata$phase. You don't need to use dataframe$variable in a tidyverse setting. Indeed that is what the pipe is seeking you to avoid doing.

    Instead you only need to do something like:

    phase <- csvdata %>%
      filter(phase == "test")
    

    Or

    mergedrows1 <- phase %>%
      group_by(subject, phase)
    

    Also, it's a bad idea to give the same name to a data frame and a variable within the data frame. R will not get confused, but you or a reader of the code will be.