Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count occurrences of values in column in R while putting different values from another column in a list

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading