0

Giving the following data:

sample <- c(rep("A",5),rep("B",7),rep("C",4),rep("D",9))
species <- sample(x = c("sp1", "sp2","sp3","sp4",NA), size = 25, replace = TRUE)
data <- data.frame(sample,species)

How can I extract the number of unique values in species by sample (A,B,C...)?

neMo
  • 3
  • 2

2 Answers2

1

We use n_distinct on a grouped data

library(dplyr)
data %>% 
    group_by(sample) %>% 
    summarise(n = n_distinct(species, na.rm = TRUE), .groups = 'drop')
akrun
  • 874,273
  • 37
  • 540
  • 662
1

A base R option

> aggregate(.~sample,data,function(x) length(unique(x)))
  sample species
1      A       3
2      B       2
3      C       3
4      D       3

or

> aggregate(.~ sample, unique(data), length)
  sample species
1      A       3
2      B       3
3      C       2
4      D       3
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81