0
    Species    Ecoregions                                                        
   <fct>      <fct>                                                             
 1 acerifolia Central Corn Belt Plains                                          
 2 acerifolia Arkansas Valley                                                   
 3 acerifolia Ouachita Mountains                                                
 4 acutidens  Sonoran Desert                                                    
 5 acutidens  Southern and Baja California Pine-Oak Mountains                   
 6 acutidens  Mojave Basin and Range                                            
 7 acutidens  California Coastal Sage, Chaparral, and Oak Woodlands             
 8 acutifolia Sierra Madre Oriental with Conifer, Oak, and Mixed Forests        
 9 acutifolia Hills and Sierra with Low Tropical Deciduous Forest and Oak Forest
10 acutifolia Hills with Medium and High Evergreen Tropical Forest     

If given a data set like the one listed above, is there anyway using either tidyverse or base r to get a tibble listing all of the ecoregions associated with each species? I figured there would be something in stringr for it, but I'm not sure how. For example:

   Species      Ecoregions
1 acerifolia    Central Corn Belt Plains, Arkansas Valley, Quachita Mountains
2 acutidens     Sonoran Desert, Southern and Baja California Pine-Oak Mountains, Mojave Basin and Range, etc. 

Along those lines. Any advice or experience?

1 Answers1

0

This should work:

library(dplyr)
your_data %>% 
  group_by(Species) %>%
  summarize(Ecoregions = toString(unique(Ecoregions)))

toString is in base. You could also use paste(..., collapse = ", "), or the stringr equivalent stringr::str_c(..., collapse = ", ").

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294