I have a dataset as such
a<- c(1,0,0,0,0)
b<- c(0,1,0,0,0)
c<- c(0,0,1,0,0)
d<- c(0,0,0,1,0)
e<- c(0,0,0,0,1)
f<- c("a", "b", "c", "d", "e")
dset<-data.frame(a,b,c,d,e,f)
dset
a b c d e f
1 1 0 0 0 0 a
2 0 1 0 0 0 b
3 0 0 1 0 0 c
4 0 0 0 1 0 d
5 0 0 0 0 1 e
I want to create a dataset that looks like this
a b c d e f g
1 1 0 0 0 0 a 1
2 0 1 0 0 0 b 1
3 0 0 1 0 0 c 1
4 0 0 0 1 0 d 1
5 0 0 0 0 1 e 1
The actual data I am using is much more complicated and much larger. That is, there is not a diagonal matrix of 1's. But the principle of what I am trying to do is captured in the idea of creating a new variable based on a set of conditions, preferably with a loop like such
variable(g) = variable(a) if variable(f) = a
variable(g) = variable(b) if variable(f) = b
The number of conditions is very large, so I would appreciate if the answer could be written with a loop.