I am attempting to convert a data frame to a more easily readable format (by humans). The current data frame (DF1) contains a list of strings (questions) in Column A, and a list of strings (categories) in Column B. Where an item fits into multiple categories, there is another row further down in DF1 with the same question in Column A, but a different category in column B.
e.g.
column1 column2 Column3
"question 1" "category A" "subcategory A"
"question 2" "category A" "subcategory B"
"question 1" "category B" "subcategory A"
I want to convert the data frame to have the question, and column B categories as column headers, with booleans indicating whether the question does or does not fall under that category.
e.g.
Question CategoryA CategoryB
"question 1" TRUE TRUE
"question 2" TRUE FALSE
I have extracted a list of unique questions from DF1 using the following:
question <- list()
for (x in DF1$column1){
if (!(x %in% question)){
question[[x]] <- x
}
}
What is the best way to do this in R? I'd prefer to avoid a series of nested loops if possible as I believe this can become very inefficient in R?
Many thanks!!