RE: [R] computing sum of indicator variables

2004-07-26 Thread Wayne Jones
What about


x <- c(2,4,7)
out<-rep(0,max(x))
out[x]<-1


Regards

Wayne



-Original Message-
From: Stefan Böhringer [mailto:[EMAIL PROTECTED] 
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

__
[EMAIL PROTECTED] 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:[EMAIL PROTECTED]http://www.kssg.com


The information in this Internet email is confidential and m...{{dropped}}

__
[EMAIL PROTECTED] 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


Re: [R] computing sum of indicator variables

2004-07-26 Thread Dimitris Rizopoulos
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" <[EMAIL PROTECTED]>
To: "R Help" <[EMAIL PROTECTED]>
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
>
> __
> [EMAIL PROTECTED] 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

__
[EMAIL PROTECTED] 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


Re: [R] computing sum of indicator variables

2004-07-26 Thread Uwe Ligges
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
__
[EMAIL PROTECTED] 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
__
[EMAIL PROTECTED] 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] computing sum of indicator variables

2004-07-26 Thread Stefan Böhringer
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

__
[EMAIL PROTECTED] 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