[R] computing sum of indicator variables

Uwe Ligges ligges at statistik.uni-dortmund.de
Mon Jul 26 11:00:02 CEST 2004


Stefan Böhringer wrote:

> My problem is as follows:
> i is a list 

Do you mean a vector?

> of integers of variable length. Now I want to compute a new
> vector/array 

A vector or an array (which dimensions?)?

 > that contains 1's at the positions indicated in i. For
> example:
> c(2, 4) -> c(0, 1, 0, 1)

How long should the result be? max(i)???

If you mean simple vectors in both cases, i's suggest:

   i <- c(2, 4)
   x <- numeric(max(i))
   x[i] <- 1

Uwe Ligges


> Using something like
> i = i - c(0, i[2:length(i) - 1]);
> sapply(i, function(x) c(rep(0, x - 1), 1)));
> 
> faces me with the problem of concatenating the result, which I could
> somehow not find a solution for.
> 
> Thank you very much in advance.
> 
> Stefan
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list