Assign intermediate output to temp variable as part of dplyr pipeline

前端 未结 5 2276
情话喂你
情话喂你 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条回答
  •  天命终不由人
    2020-11-30 13:05

    This does not create an object in the global environment:

    df %>% 
       filter(b < 3) %>% 
       { 
         { . -> tmp } %>% 
         mutate(b = b*2) %>% 
         bind_rows(tmp) 
       }
    

    This can also be used for debugging if you use . ->> tmp instead of . -> tmp or insert this into the pipeline:

    { browser(); . } %>% 
    

提交回复
热议问题