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
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
----- Original Message -----
From: "Stefan B侖hringer" <commercial at s-boehringer.de>
To: "R Help" <r-help at stat.math.ethz.ch>
Sent: Monday, July 26, 2004 10:45 AM
Subject: [R] computing sum of indicator variables
> 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 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
Stefan B??hringer wrote:> My problem is as follows: > i is a listDo you mean a vector?> of integers of variable length. Now I want to compute a new > vector/arrayA 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
What about
x <- c(2,4,7)
out<-rep(0,max(x))
out[x]<-1
Regards
Wayne
-----Original Message-----
From: Stefan Böhringer [mailto:commercial@s-boehringer.de]
Sent: 26 July 2004 09:46
To: R Help
Subject: [R] computing sum of indicator variables
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
KSS Ltd
Seventh Floor St James's Buildings 79 Oxford Street Manchester M1 6SS
England
Company Registration Number 2800886
Tel: +44 (0) 161 228 0040 Fax: +44 (0) 161 236 6305
mailto:kssg@kssg.com http://www.kssg.com
The information in this Internet email is confidential and m...{{dropped}}