0

I´m trying to create new variables from the options of one I have in my dataframe. This is my initial dataframe:

d1 <- data.frame("id" = c(1,1,2,2,3,4,5), "type" = c("A","B","C","C","A","B","C"))

  id  type
1  1    A
2  1    B
3  2    C
4  2    C
5  3    A
6  4    B
7  5    C

So, if would like to create new variables depending of the value of "type" for each id, I would like to get this kind of dataframe:

d2 <- data.frame("id" = c(1,1,2,2,3,4,5), "type" = c("A","B","C","C","A","B","C"),
                 "type.A" = c(1,0,0,0,1,0,0), "type.B" = c(0,1,0,0,0,1,0),
                 "type.C" = c(0,0,1,1,0,0,1))

  id type type.A type.B type.C
1  1    A      1      0      0
2  1    B      0      1      0
3  2    C      0      0      1
4  2    C      0      0      1
5  3    A      1      0      0
6  4    B      0      1      0
7  5    C      0      0      1

The idea is give 1 in the new variable (type.A in this case) if the "type" of an specific "id" is equal to A, if else give 0. Since this is a common problem in big data analysis (I think), I would like to know if there is a function to solve this problem.

Luis M. García
  • 123
  • 2
  • 9

1 Answers1

0
cbind(d1, setNames(data.frame(+sapply(unique(d1$type), function(x)
    d1$type == x)), unique(d1$type)))
#  id type A B C
#1  1    A 1 0 0
#2  1    B 0 1 0
#3  2    C 0 0 1
#4  2    C 0 0 1
#5  3    A 1 0 0
#6  4    B 0 1 0
#7  5    C 0 0 1
d.b
  • 32,245
  • 6
  • 36
  • 77