2

I want summarize the following data frame to a summary table.

plot <- c(rep(1,2), rep(2,4), rep(3,3))
bird <- c('a','b', 'a','b', 'c', 'd', 'a', 'b', 'c')
area <- c(rep(10,2), rep(5,4), rep(15,3))
birdlist <- data.frame(plot,bird,area)
birdlist
  plot bird area
1    1    a   10
2    1    b   10
3    2    a    5
4    2    b    5
5    2    c    5
6    2    d    5
7    3    a   15
8    3    b   15
9    3    c   15

I tried the following

birdlist %>% 
  group_by(plot, area) %>% 
  mutate(count(bird))

I am trying to get a data frame as result that looks like the following

plot bird area
   1    2   10
   2    4   5
   3    3   15

Please help/advice on how to count bird with reference to plot and respective area of the plot. Thanks.

2 Answers2

4

You were very close, you want summarize instead of mutate though and you can use n() to count the number of rows within the group you're specifying.

library(tidyverse)
birdlist %>%
  group_by(plot, area) %>%
  summarize(bird = n(),
            .groups = "drop")
#> # A tibble: 3 x 3
#>    plot  area  bird
#>   <dbl> <dbl> <int>
#> 1     1    10     2
#> 2     2     5     4
#> 3     3    15     3

If you're set on count, you would use it without group_by.

birdlist %>%
  count(plot, area, name = "bird")
TrainingPizza
  • 1,090
  • 3
  • 12
  • Thanks, I tried that but I was getting an error. ```birdlist %>% + group_by(plot, area) %>% + summarize(bird = n(), + .groups = "drop") Error: `n()` must only be used inside dplyr verbs. Run `rlang::last_error()` to see where the error occurred.``` – Saneesh C S Dec 10 '21 at 23:54
  • Ah, you probably have conflicting packages. run it as dplyr::summarize – TrainingPizza Dec 10 '21 at 23:56
  • I still have the problem. – Saneesh C S Dec 10 '21 at 23:57
  • Done!!! Thanks a lot. – Saneesh C S Dec 10 '21 at 23:58
  • No problem. When you load packages you'll get a warning which function names have a conflict so make sure to look at that. I get this often with `dplyr` and `rms` for the `summarize` function. When that happens you have to specifically reference the package you want to call the function from (i.e. dplyr::summarize) or R will use whatever the most recent loaded package was. – TrainingPizza Dec 11 '21 at 00:01
3

We could group_by plot and summarise using unique():

birdlist %>% 
  group_by(plot) %>% 
  summarise(bird = n(), area = unique(area))
     plot  bird  area
  <dbl> <int> <dbl>
1     1     2    10
2     2     4     5
3     3     3    15
TarJae
  • 72,363
  • 6
  • 19
  • 66