Home > Software design >  Creating dummy variables based on string variables in R
Creating dummy variables based on string variables in R

Time:06-22

Suppose I have data as follows:

people <- c("John", "Paul", "George", "Ringo", "Mick", "Keith", "Bill", "Charlie", "Brian")

colors <- c("Red", "Blue", "Green", "Red", "Red", "Green", "Green", "Blue", "Blue")

df <- cbind(people, colors)

How can I transform my df such that it extracts the content from the "colors" column and makes it into columns, as below:

Red <- c(1, 0, 0, 1, 1, 0, 0, 0, 0)
Blue <- c(0, 1, 0, 0, 0, 0, 0, 1, 1)
Green <- c(0, 0, 1, 0, 0, 1, 1, 0, 0)

df_dummies <- do.call("cbind", list(people, Red, Blue, Green))
df_dummies

Thanks!

CodePudding user response:

data.frame(
  people = people,
  Red = as.integer(colors=="Red"),
  Green = as.integer(colors=="Green"),
  Blue = as.integer(colors=="Blue")
           )

CodePudding user response:

Base R solution:

with(
  data.frame(df),
  cbind(
    people = people,
    setNames(
      data.frame( outer(colors, unique(colors),`==`)),
      unique(colors)
    )
  )
)
  • Related