Using the mutate function in R

What is the easiest/best way to use the mutate() function in R?

See below for the best way :grinning:
If you tune your question, I can try to tune my answer

suppressPackageStartupMessages(library(dplyr))

df1 <- data.frame(
  x = 1:3
)

df2 <- df1 |>
  mutate(
    y = 2*x,
    z = case_when(
      y <  3 ~ "small",
      y == 4 ~ "fair",
      TRUE   ~ "must be large"
    )
  )

print(df2)
#>   x y             z
#> 1 1 2         small
#> 2 2 4          fair
#> 3 3 6 must be large
Created on 2022-11-29 with reprex v2.0.2

Thanks for that. How about using mutate() to create new columns form an existing data frame? Also, how can summarize() be used to make summaries if total sums/averages? Thanks so much.

You can learn the basics of data wrangling with the tidyverse family of packages by reading this free ebook

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.