# Organize data with multiple levels of a categorical variable per entry, for easy R analysis

I need some help here.

I have some data in which every entry can take one or more levels of a categorical variable. for example, I have a category with 3 levels:

entry   category
1       A, B, C
2       B
3       C, A, B


How should I organized it into a file in order to easily let R discriminate among levels in order to do my analysis. Something like this:

Categories     entries    other_results
A              2          ~
B              3          ~
C              2          ~


I thought about doing a comma separated list, as shown in the example 1. But then what should I do in R in order to transform those string into categories?

CLARIFICATION: I'd like to avoid creating a column for every level. This variable has many levels and many other variables are already present. This would make the file uselessly big and not readable easily by humans.

thanks!

## 2 Answers

After OP's clarification, it's clear that this is not the solution OP would like to use. I leave it here because the comment thread develops OP's thinking further.

I would make a different column for each category. So ColA takes on 1 when the entry belongs to A and 0 otherwise, ColB takes on 1 when the entry belongs to B and 0 otherwise, and so on.

• I justed hoped to be able to avoid this solution... I have 15 categories, in addition to many other variables and wanted to avoid adding more columns, also to make the data more understandable visually. Sure there is a R solution to avoid multicolumning? – Bakaburg May 28 '14 at 15:24
• Well, factors might be able to avoid this problem. If you want to represent all 15 of your categories in a single column, you could code A={A}, B={B}, AB={A and B}, ABC={A,B, and C}, etc. But doing this for all 15 categories doesn't really seem like a huge improvement over creating 15 individual columns. Also, it will destroy, from R's POV, the association between, e.g., A and AB and ABC. That's why people tend to prefer the one-column-per-category approach. – Sycorax says Reinstate Monica May 28 '14 at 15:30
• No, I need to analyze each level separately. One solution I was thinking of was keeping the levels as a string in the dataframe, transforming it in a vector with the levels of every entry, creating another unique vector with all the levels and then analyze the data by looping through the all levels vector and selecting the entries which contains the level searched. – Bakaburg May 28 '14 at 15:45
• I hoped in a more native solution. – Bakaburg May 28 '14 at 15:45
• @Bakaburg To each his own, but it truly seems to be inordinately complex to write such a loop than to add additional columns. It seems that you could retain your columns with the free-text categories and also have the additional binary columns. Though redundant, both representations are useful for different ends. – Sycorax says Reinstate Monica May 28 '14 at 15:53

See the R Hmisc package mChoice function.