0

I have a dataset of species from different years, but not all species was seen in each year. years are listed in rows in the data frame, but I want to group by species, then pull a vector of years when that species was present. Not sure how to go about it.

df2 <- 
  data.frame(species = rep(c("a","b","c"),each=10),
             year = c(1991:2000),
             value = runif(30,0,100))[-c(5,7,11,12,16,18),] # remove some years
rownames(df2) <- c(c(1:nrow(df2)))

df2 %>% group_by(species) %>%
  count() # see that they are present for different numbers of years

desired output:


  species     years_present
1 a           1991, 1992, 1993, 1994, 1996, 1998, 1999, 2000
2 b           1993, 1994, 1995, 1997, 1999, 2000
3 c           1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000

Jake L
  • 815
  • 4
  • 13
  • 1st link would convert to a single delimited string, 2nd link would create a vector see this answer for dplyr - https://stackoverflow.com/a/51775575/680068 – zx8754 Oct 27 '21 at 08:43

0 Answers0