Home > Software design >  Count occurrences of values in column in R while putting different values from another column in a l
Count occurrences of values in column in R while putting different values from another column in a l

Time:09-09

I have a table in R that looks something like this:

animal | name
------ | ----
'dog'  |  'Airbud' 
'cat'  |  'Tom' 
'dog'  |  'Marley' 
'cat'  |  'Salem' 
'cat'  |  'Salem' 
'rabbit'  | 'bugs'

I am interested in grouping and counting the data based off the 'animal' column, while still preserving the unique values in the 'name' column as a list such that the output would look something like this.

animal | name_list | animal_count
------ | ---- | ------------
'dog'  |  ['Airbud','Marley'] | 2
'cat'  |  ['Tom','Salem'] | 3
'rabbit'  | ['bugs'] | 1

The function would be very similar to df %>% group_by(animal) %>% count(), but it would also have the added functionality of including a list of unique names associated with each animal.

CodePudding user response:

Using tidyverse, you could do:

library(tidyverse)

df %>%
  group_by(animal) %>%
  summarize(name_list = paste(unique(name), collapse = ", "), 
            animal_count = n())
#> # A tibble: 3 x 3
#>   animal name_list      animal_count
#>   <chr>  <chr>                 <int>
#> 1 cat    Tom, Salem                3
#> 2 dog    Airbud, Marley            2
#> 3 rabbit bugs                      1

Data from question in reproducible format

df <- structure(list(animal = c("dog", "cat", "dog", "cat", "cat", 
"rabbit"), name = c("Airbud", "Tom", "Marley", "Salem", "Salem", 
"bugs")), row.names = c(NA, -6L), class = "data.frame")

df
#>   animal   name
#> 1    dog Airbud
#> 2    cat    Tom
#> 3    dog Marley
#> 4    cat  Salem
#> 5    cat  Salem
#> 6 rabbit   bugs

Created on 2022-09-08 with reprex v2.0.2

  •  Tags:  
  • r
  • Related