I have the following dataframe
df <- data.frame(x=c("one", "one, two", "two, three", "one, two, three"))
It looks like this
x
1 one
2 one, two
3 two, three
4 one, two, three
I would like to be able to separate this x
column into many different columns, one for every distinct word in the column x
. Basically I would like the final result to be something like this
one two three
1 1 0 0
2 1 1 0
3 0 1 1
4 1 1 1
I think that in order to obtain that dataframe, I probably need to be able to use the separate
function provided by tidyr
and documented here. However, this requires knowledge of regular expressions, and I'm not good with them. Can anyone help me obtain this dataframe?
IMPORTANT: I do not know the number, nor the spelling of the words a priori.
Important Example
It should work also with empty strings. For instance if we have
df <- data.frame(x=c("one", "one, two", "two, three", "one, two, three", ""))
then it should also work.