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!