-1

I am attempting to convert the rows of org_type into columns. Is there a way to do this I tried spread, but I have not had success. Does anyone know how? Below is a picture. r

  • 1
    Please show a small reproducible example with `dput` with expected output – akrun May 07 '20 at 22:50
  • Please see how to create a [reproducible minimal example](https://stackoverflow.com/q/5963269/8107362). – mnist May 08 '20 at 00:06
  • This might help https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format . Also please don't share data using images use `dput` instead. Read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah May 08 '20 at 03:56

1 Answers1

0

You can do it using mltools and data.table R package. First you need to convert the data frame to data.table. Then you've to use one_hot() function of mltools. You can see the example I've used with a dummy data.

# demo data frame
df <- data.frame(
  org_name = c("A", "B", "C", "D", "E", "F"),
  org_type = c("Tech", "Tech", "Business", "Business", "Bank", "Bank")
)
df
# load the libraries
library(data.table)
library(mltools)

# convert df to data.table
df_dt <- as.data.table(df)
# use one_hot specify the column name in cols
df_one_hot <- one_hot(df_dt, cols = "org_type")
df_one_hot

Output before:

    org_name org_type
1        A     Tech
2        B     Tech
3        C Business
4        D Business
5        E     Bank
6        F     Bank

Output after:

 org_name org_type_Bank org_type_Business org_type_Tech
1:        A             0                 0             1
2:        B             0                 0             1
3:        C             0                 1             0
4:        D             0                 1             0
5:        E             1                 0             0
6:        F             1                 0             0
ranamahmud
  • 136
  • 2
  • 4