-4
a <- c(1,2,1,1,1,1,1,2,3,4,1,1,1,1,1,1,1,1)
b <- c("A", "B", "A", "A", "A", "A", "A", "A", "B","A", "B", "A", "A", "A", "A", "A", "A", "B")
c <- cbind(a,b)
df <- data.frame(c)

What is the simplest way to count distinct elements in a data.frame column?

pogibas
  • 27,303
  • 19
  • 84
  • 117
Dataman
  • 11
  • 1
  • 3
    Please provide wanted output, it's hard to understand what you mean by "instances", "different value" and "distinct elements" – pogibas Apr 11 '18 at 08:24
  • 3
    `help("table")`. Also, don't use `cbind` if you want a data.frame. Simply do `data.frame(a, b)`. – Roland Apr 11 '18 at 08:25

1 Answers1

0

Something like this?

> table(df)
   b
a    A  B
  1 12  2
  2  1  1
  3  0  1
  4  1  0

> table(df$a)

 1  2  3  4 
14  2  1  1 

> table(df$b)

 A  B 
14  4 
mjimcua
  • 2,781
  • 3
  • 27
  • 47