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
Hopefully category is a factor variable. If not, convert it to factor:
category <- as.factor(category)
You could use the relevel function to assigned level 1 to the category "dog" as follows:
levels(category) <- relevel(category, ref = "dog")
and then create a data frame using following code:
df <- data.frame(category,as.numeric(category))
colnames(df) <- c("category","rank")
as.numeric
function returns the levels of the factors which is the rank in your case.