Why can't you do: df0 |> mutate( ... ) |> mutate( ... ) |> mutate( ... )
I've simplified the code to show passing the result of the first line to the next rather than focussing on the detail. This would work with %>% as well as |> but I am anticipating that the more modern native pipe ( |> ) will become the norm in the future. BTW - mutate_at is depreciated as far as I know, so you'd be more future proof to use mutate( across( ... ) ) You can use mutate more than one thing in a single mutate df |> mutate ( surname = to.upper(surname), initial = substring(first name, 1, 1) ) is the same as: df |> mutate ( surname = to.upper(surname)) |> mutate ( initial = substring(first name, 1, 1) ) Or df |> mutate ( surname = to.upper(surname) ) -> df2 df2 |> mutate ( initial = substring(first name, 1, 1) ) On Thu, 17 Oct 2024, 22:51 Sparks, John, <jspa...@uic.edu> wrote: > Hi R Helpers, > > I have been looking for an example of how to execute different dplyr > mutate statements on the same dataframe in a single step. I show how to do > what I want to do by going from df0 to df1 to df2 to df3 by applying a > mutate statement to each dataframe in sequence, but I would like to know if > there is a way to execute this in a single step; so simply go from df0 to > df1 while executing all the transformations. See example below. > > Guidance would be appreciated. > --John J. Sparks, Ph.D. > > library(dplyr) > df0<-structure(list(SeqNum = c(1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, > 11L, 12L, 13L, 14L, 15L, 16L, 18L, 19L, 21L, 22L, 23L), MOSTYP = c(37L, > 41L, 41L, 13L, 3L, 27L, 37L, 37L, 15L, 14L, 13L, 37L, 4L, 27L, > 37L, 26L, 17L, 37L, 37L, 17L), MGEMOM = c(1L, 1L, 1L, 1L, 1L, > 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L), > MGODRK = c(3L, 2L, 2L, 3L, 4L, 2L, 2L, 2L, 3L, 4L, 3L, 2L, > 3L, 1L, 2L, 3L, 4L, 4L, 3L, 3L), MOSHOO = c(7L, 7L, 7L, 2L, > 9L, 4L, 7L, 7L, 2L, 2L, 2L, 7L, 9L, 4L, 7L, 4L, 2L, 7L, 7L, > 2L), MRELGE = c(0L, 1L, 0L, 2L, 1L, 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>% > mutate(across(starts_with('P'),~ifelse(.x==0, 0, > ifelse(.x==1, 25, > ifelse(.x==2, 75, > ifelse(.x==3, 150, > ifelse(.x==4, 350, > ifelse(.x==5, 750, > ifelse(.x==6, 3000, > ifelse(.x==7, 7500, > ifelse(.x==8,15000, > ifelse(.x==9,30000, > -99)))))))))))) > > df2<-df1 %>% > mutate_at(vars(MRELGE:MSKC),~ifelse(.x==0, 0, > ifelse(.x==1, 5, > -99))) > df3<-df2 %>% > mutate_at(vars(MGODRK),~ifelse(.x==0, 0, > ifelse(.x==1, 5, > -99))) > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.