0

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.

w.kye
  • 17
  • 2
  • Intead of a loop, try the `apply` family of functions, or consider another fast way to iterate over your data set like `data.table` using the assignment operator `:=` – Gary Weissman Mar 16 '17 at 17:55
  • In this instance. you'd use `dset[head(names(dset), -1)][cbind(seq_along(dset$f), match(dset$f, names(dset)))]` or `as.integer(dset[cbind(seq_along(dset$f), match(dset$f, names(dset)))])`. – lmo Mar 16 '17 at 18:08
  • Great the second one work! The first option returned "Error in as.matrix(x)[i] : subscript out of bounds". Not sure why though – w.kye Mar 16 '17 at 18:25

0 Answers0