How do I generate a count in R within mutate()?

Hi @Qomisa and welcome! Thanks for sharing your sample data and your expected output.

You were on the right track with using mutate() to create a new column, but there is actually a built in function in dplyr to count the number of rows per group -- it is called n(). In your example, you wanted to get the number of rows per P_ID, so you need to group by that variable and then create a new count variable.

library(dplyr)

df <- structure(list(P_ID = c(18, 1, 4, 5, 10, 12, 2, 5, 2, 10, 4, 
4, 8, 9, 7, 17, 7, 19, 9)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -19L))

df %>% 
  group_by(P_ID) %>% 
  mutate(count = n())
#> # A tibble: 19 x 2
#> # Groups:   P_ID [12]
#>     P_ID count
#>    <dbl> <int>
#>  1    18     1
#>  2     1     1
#>  3     4     3
#>  4     5     2
#>  5    10     2
#>  6    12     1
#>  7     2     2
#>  8     5     2
#>  9     2     2
#> 10    10     2
#> 11     4     3
#> 12     4     3
#> 13     8     1
#> 14     9     2
#> 15     7     2
#> 16    17     1
#> 17     7     2
#> 18    19     1
#> 19     9     2

Created on 2020-05-05 by the reprex package (v0.3.0)

2 Likes