R:发生次数 - >二进制序列?

and*_*kos 0 binary r

将非递减seq的出现次数转换为0-1 seq有哪些更好的选择?谢谢.

d<-c(3,5,9,12,15);
c(rep(0,d[1]-1),1,unlist(rbind(mapply(rep,0,diff(d)-1),1)))
Run Code Online (Sandbox Code Playgroud)

rcs*_*rcs 6

我认为这应该做同样的事情

d <- c(3,5,9,12,15);   
x <- integer(max(d))    # initialize integer vector where all entries are zero;
                        # length(x) = max(d) (or last element of d)
x[d] <- 1L              # set x to 1 at the position of each occurence           
Run Code Online (Sandbox Code Playgroud)