tags:

views:

92

answers:

1

What are the better options to convert a non-decreasing seq of occurrence times to a 0-1 seq? Thanks.

d<-c(3,5,9,12,15);
c(rep(0,d[1]-1),1,unlist(rbind(mapply(rep,0,diff(d)-1),1)))
+6  A: 

I think this should do the same

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
rcs
good answer! may be a comment or two will help the uninitiated.
Eduardo Leoni
Why do you need both 1 and L? For my purpose, it's best to let L be formed by either ones or (random) sizes of events when they occur. So the answer is not good, but very good!
knot
just for memory efficiency ;) with 1L the class of the array is integer, otherwise it is numeric; see, e.g., class(1L) and class(1)
rcs