-2

Is there a fast way to split a column into many columns based on the values?
For example the column x has the values (a, b, c, d). Now I want to split the column x into 4 columns (a,b,c,d) with the value 0 or 1. Like in the following example:

x | a b c d
a | 1 0 0 0
c | 0 0 1 0
b | 0 1 0 0
b | 0 1 0 0
d | 0 0 0 1

  • 1
    Please use `dput()` to show your data or a data definition. – jogo Jan 19 '18 at 14:35
  • How do you go from `c(1, 2, 3, 4)` to that table? – Rui Barradas Jan 19 '18 at 14:37
  • Sounds like a job for `dummy` package: https://cran.r-project.org/web/packages/dummies/dummies.pdf This is a simple example `library(dummies); x = c("a","a","b","c"); dummy(x)` – AntoniosK Jan 19 '18 at 14:39
  • Sorry I made a mistake. The values in the columns are a, b, c and d. Out of them I want to create a new column for each different value in the original column. If row 1 had an a in column x, I want to create a column a (with the value 1 in this case), b (with the value zero), c (0) and d (0) – user5317046 Jan 19 '18 at 14:41
  • 1
    Suggested dupe: [How to create dummy variables in R](https://stackoverflow.com/q/11952706/903061) – Gregor Thomas Jan 19 '18 at 14:47
  • This seems to be a *reshape* question which probably has been asked several times before. One possible solution: `x <- c("a", "c", "b", "b", "d"); library(data.table); dcast(data.table(x)[, rn := .I], rn + x ~ x, length)[, rn := NULL][]` – Uwe Jan 19 '18 at 15:08

3 Answers3

0

Try this:

as.data.frame(do.call(rbind,lapply(df$x,table)))
Shenglin Chen
  • 4,504
  • 11
  • 11
0

Various options here:

x = c("a","c","b","b","d")

library(dummies)

dummy(x)

#      xa xb xc xd
# [1,]  1  0  0  0
# [2,]  0  0  1  0
# [3,]  0  1  0  0
# [4,]  0  1  0  0
# [5,]  0  0  0  1

dummy.data.frame(data.frame(x))

#   xa xb xc xd
# 1  1  0  0  0
# 2  0  0  1  0
# 3  0  1  0  0
# 4  0  1  0  0
# 5  0  0  0  1


library(tidyverse)

data.frame(x) %>%
  mutate(id = row_number(),
         counts = 1) %>%
  spread(x, counts, fill = 0) %>%
  select(-id)

#   a b c d
# 1 1 0 0 0
# 2 0 0 1 0
# 3 0 1 0 0
# 4 0 1 0 0
# 5 0 0 0 1
AntoniosK
  • 15,991
  • 2
  • 19
  • 32
0

For digit values it would work like this:

install.packages("splitstackshape")
library(splitstackshape)
splitted <- cSplit_e(data, "x", "|", type = "numeric", fill = 0, drop = T)

For all values it will work like this (but you have to delete the original column afterwards):

install.packages("dummies")
library(dummies)
splitted <- cbind(data, dummy(data$x, sep = "_"))