1

Doing some computation and I've noticed that for further manipulation I'd need to preserve dataframe in a prior state. So, I have a bunch of dplyr steps and in the middle of pipe chain I would like to preserve data frame state into other object

is this possible?

df <- read.csv(file) %>%
      mutate(....) %>%
      mutate(....) %>%
      # save df state to new df2 object here.......*****
      group_by(....) %>%
      arrange(var) %>%
      summary()
Forge
  • 1,587
  • 1
  • 15
  • 36

1 Answers1

3

Try the following code:

df <- read.csv(file) %>%
  mutate(....) %>%
  mutate(....)

# save df state to new df2 object here.......*****
df2 <- df

df %>% group_by(....) %>%
  arrange(var) %>%
  summary()

Inside the pipe chain as requested:

df <- read.csv(file) %>%
  mutate(....) %>%
  mutate(....) %>%
  {df2 <<- .} %>% # save df state to new df2 object here.......*****
  group_by(....) %>%
  arrange(var) %>%
  summary()

Hope it helps!

tk3
  • 990
  • 1
  • 13
  • 18