what is difference between 'mutate' and 'summarise'

I'm learning R language with book. Now, I'm learning part of using group by and summarise. But i can't find difference between mutate and summarise. I think both function are same that they are add variable. So Could you help me?

mutate() either changes an existing column or adds a new one.

summarise() calculates a single value (per group).

1 Like

To add an example to what @martin.R said:

library(magrittr)

tibble::as_tibble(mtcars) %>%
  dplyr::select(cyl) %>%
  dplyr::mutate(new_cyl = cyl * 2)
#> # A tibble: 32 x 2
#>      cyl new_cyl
#>    <dbl>   <dbl>
#>  1     6      12
#>  2     6      12
#>  3     4       8
#>  4     6      12
#>  5     8      16
#>  6     6      12
#>  7     8      16
#>  8     4       8
#>  9     4       8
#> 10     6      12
#> # … with 22 more rows

tibble::as_tibble(mtcars) %>%
  dplyr::select(cyl) %>%
  dplyr::group_by(cyl) %>%
  dplyr::summarize(n = n())
#> # A tibble: 3 x 2
#>     cyl     n
#>   <dbl> <int>
#> 1     4    11
#> 2     6     7
#> 3     8    14

Created on 2019-02-05 by the reprex package (v0.2.1)
As you can see, in the first example, new column is added. In the second, I group by cyl and then create a summary with summarize for each group in cyl.

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.