Changing categorical data to numeric according to other categorical data

Hello, I have data on species in different state parks. There are four main columns: Region (categorical), Park (categorical), Nativeness (categorical), Organism (Categorical). The ultimate goal is to run a two way ANOVA on the dataset, but to do that I need to make the Nativeness category numeric according to park. In the end I want a number of native species for each park. I tried using

data$category <- as.numeric(data$category) 

but that just turned all of the "natives" in the nativeness column into the same number rather than adding them up according to national park before turning the data into numeric.
Thanks for your help!

When you say turn the categorical into numerical, do you mean you want counts for each category? Or do you mean you want to use a number to represent the category?


To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

I meant the counts for each category - thanks! So for example there would be a total of say 830 native species in one park.

Sure, here's an example of the data set (hopefully the formatting works)

    Region    Park   Nativeness   Organism
1 Mid Eastern  XXX    Native   AAA
2 Mid Eastern  ZZZ    Native   BBB
3 Mid Eastern  XXX    Native   CCC
4 Mid Eastern  RRR    Native   AAA
5 Mid Eastern  XXX    Native   CCC

So with this set, I want the program to sort the data so that I have this information (not in this format):

Park XXX has 3 native species, organisms AAA, CCC, CCC
Park ZZZ has one native, organism BBB
Park RRR has one native species, organism AAA
All Parks still have data on which organisms


You had a good instinct to share something about the data of concern.

However, the way you did it does not facilitate easy copy and pasting of your data, so a user can easily add it to their R session.

You can read this guide to see how such things are possible via datapasta package, or base::dput()
FAQ: How to do a minimal reproducible example ( reprex ) for beginners

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.