Search code examples
rdatasettibblespread

How do you turn categorical rows into columnd?


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


Solution

  • 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