I want summarise a data frame

Viewed 43

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 Answers

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")

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
Related