Not sure why one will need it. But as @Frank suggested one option is to use %T>%
operator (tee operator
) from magrittr
package along with assign
function to store intermediate values.
In the below code the SummaryVal
will have summary
information of cars
and MyValue
will hold the intermediate value after mutate
.
library(tidyverse)
library(magrittr)
SummaryVal <- cars %>% mutate(kmh = dist/speed) %T>%
assign("MyValue",.,envir = .GlobalEnv) %>%
summary()
head(MyValue)
# speed dist kmh
# 1 4 2 0.5000000
# 2 4 10 2.5000000
# 3 7 4 0.5714286
# 4 7 22 3.1428571
# 5 8 16 2.0000000
# 6 9 10 1.1111111
SummaryVal
# speed dist kmh
# Min. : 4.0 Min. : 2.00 Min. :0.500
# 1st Qu.:12.0 1st Qu.: 26.00 1st Qu.:1.921
# Median :15.0 Median : 36.00 Median :2.523
# Mean :15.4 Mean : 42.98 Mean :2.632
# 3rd Qu.:19.0 3rd Qu.: 56.00 3rd Qu.:3.186
# Max. :25.0 Max. :120.00 Max. :5.714
UPDATED:
As @Renu correctly pointed out even %>%
will work as below:
SummaryVal <- cars %>% mutate(kmh = dist/speed) %>%
assign("MyValue",.,envir = .GlobalEnv) %>%
summary()