How do I put multiple entries (in one cell within a column) as separate columns? (details provided)

Hi everyone,
I have a dataset and I have a column named "fashion.course" that looks like this on my data tab (for row one) for example: Fashion A catalogue plan Sewing up

When I call the variable/column "fashion.course" in R: is shows up as:

[1] "Fashion\nA catalogue plan\nSewing up"                                                                              
[2] "Fashion\nA catalogue plan\nSewing up"                                                                              
[3] "Fashion\nDrying material\nA catalogue plan\nTearing\nSewing up\nHemming\nDrying material to sew."

The column/variable "fashion.course" can have a different combinations as a result of respondents choosing for 14 answers(check all that apply question).

When I run R, I want each of the responses within the column to be placed in its respective new column as 1 or 0.I want all 14 answers to have its own column.

For example,I want it my data to look like this when it is done:

fashion.course                                        Fashion  A catalogue plan  Sewing up  Tearing......all 14 answers
Fashion A catalogue plan Sewing up          1               1                           1               0

Would you be able to help me find the appropriate way to achieve something like this?

You can use grepl() to search for character strings and ifelse() to create binary variables
You need to write 14 lines of code: 1 for each new binary variable that you want

df$Fashion <- ifelse(grepl("Fashion\", df$fashion.course),1,0)
df$Catalogue <- ifelse(grepl("catalogue", df$fashion.course), 1, 0)

etc.

Thank you Erica for your help. Is df an example of dataset?

Lately will the grepl work for phrase with spaces like "A catalgue plan"

Lastly, my output as noted before comes out like "\n" will using grepl() and ifese () do anything about it? Thank you once again.

Yes replace "df" with the name of your data frame. And yes it will work with spaces. If you want to keep spaces in your column names use `` for example

df$`Drying material to sew `

Also you don't need to use the full name of the course with grepl(), any unique string of characters within the name will do, for example

df$`Drying  material to sew` <- ifelse(grepl("Drying material", df$fashion.course),1,0)

as long as none of the other course names contain the phrase "Drying material". does that make sense?

Yes that make sense, thank you so much Erica

Hi Erica, I have another question that has to do with the binary solution you recommended.I was not sure if I am suppose create another question.

I want to be able to add the number of "1" s in each row into a another column called "total". I used the code:

wp <- wp %>%
mutate(sum.fashioncoursetotal=sum(wp$Fashion,wp$Acatalogueplan,wp$Sewingup,wp$Dryingmaterial,wp$Tearing,wp$Hemming,wp$Dryingmaterialtosew.,wp$Notesinfashion,wp$Other))

a got a total of 13 for all my rows, I was hoping to get different number depending on the different 1s in the row? Do you kindly help me with this?

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