-1

I have this dataframe:
CLAIM CODE
a 1
a 2
a 3
b 1
b 2
c 3

And i want this dataframe:

CLAIM CODE1 CODE2 CODE3
a 1 1 1
b 1 1 0
c 0 0 1

one-hot encoding either encodes the both columns or removes the encoded column from the original dataset. I've also tried spread() but it doesn't quite get me all the way to my target dataframe

Cindy M.
  • 11
  • 1
  • 1
    You should edit your question to reflect the structure of your dataframe (columns / rows) and of the expected output. As it is, we can see what is columns and what is rows – dc37 Nov 26 '19 at 18:11
  • The question is not clear, I do not understand how rows and columns are supposed to match – Alex Nov 26 '19 at 18:32
  • Does this answer your question? [Easy way to convert long to wide format with counts](https://stackoverflow.com/questions/34417973/easy-way-to-convert-long-to-wide-format-with-counts) – camille Nov 26 '19 at 19:08

1 Answers1

3

We can use table from base R

table(df1)
#  CODE
#CLAIM 1 2 3
#    a 1 1 1
#    b 1 1 0
#    c 0 0 1
akrun
  • 874,273
  • 37
  • 540
  • 662