12

I am trying to make a summary table of many strings. My data looks like this:

x<-c("a", "a", "b", "c", "c", "c", "d")

How would I analyse the recurrence of each string at once? Ideally to produce a table of frequency like this (I presume it would be easy to sort for decreasing frequency):

"a" 2
"b" 1
"c" 3
"d" 1
zx8754
  • 52,746
  • 12
  • 114
  • 209
bac
  • 607
  • 2
  • 8
  • 18

2 Answers2

19

Use this to make the frecuency table:

table(x)

To sort just use sort.

sort(table(x), decreasing = TRUE)

Hope that helps

Luciano Selzer
  • 9,806
  • 3
  • 42
  • 40
5

Similarly,

rle(sort(x))

will do the counting; you can then sort the results as desired.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73