Re: [R] computing sum of indicator variables

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.ac.be>
Date: Mon 26 Jul 2004 - 18:59:34 EST

Hi Stefan,

you could try something like,

x <- c(2,4,7)
as.numeric(!is.na(match(seq(1, max(x)), x)))

I hope this helps.

Best,
Dimitris



Dimitris Rizopoulos
Doctoral Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


> My problem is as follows:
> i is a list of integers of variable length. Now I want to compute a
new
> vector/array that contains 1's at the positions indicated in i. For
> example:
> c(2, 4) -> c(0, 1, 0, 1)
>
> 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@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



R-help@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 Received on Mon Jul 26 19:11:18 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:55:13 EST