Assign intermediate output to temp variable as part of dplyr pipeline

前端 未结 5 2263
情话喂你
情话喂你 2020-11-30 12:10

Q: In an R dplyr pipeline, how can I assign some intermediate output to a temp variable for use further down the pipeline?

My approach below works. But it assigns in

5条回答
  •  萌比男神i
    2020-11-30 12:43

    pipeR is a package that extends the capabilities of the pipe without adding different pipes (as magrittr does). To assign, you pass a variable name, quoted with ~ in parentheses as an element in your pipe:

    library(dplyr)
    library(pipeR)
    
    df %>>%
      filter(b < 3) %>>%
      (~tmp) %>>% 
      mutate(b = b*2) %>>%
      bind_rows(tmp)
    ##   a b
    ## 1 A 2
    ## 2 B 4
    ## 3 A 1
    ## 4 B 2
    
    tmp
    ##   a b
    ## 1 A 1
    ## 2 B 2
    

    While the syntax is not terribly descriptive, pipeR is very well documented.

提交回复
热议问题