Here is my problem:
I have a table with categories and I want to rank them:
category
dog
cat
fish
dog
dog
What I want is to add
I assume that if you write "ranks" you mean ranks. I further assume you want to rank according to number of occurrence.
cats <- factor(c("dog", "cat", "fish", "dog", "dog"))
#see help("rank") for other possibilities to break ties
ranks <- rank(-table(cats), ties.method="first")
DF <- data.frame(category=cats, rank=ranks[as.character(cats)])
print(DF)
# category rank
# 1 dog 1
# 2 cat 2
# 3 fish 3
# 4 dog 1
# 5 dog 1