[R] A sample question

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Apr 30 23:00:34 CEST 2002


"Ambrosini Alessandro" <klavan at tiscalinet.it> writes:

> Hello.
> Given a vector 1 3 4 2 8 9 5
> I want to obtain a vector with all 0 except in the second position and in
> the fifth, where the numbers are the same of the first vector.
> The new vector must be
>  0 3 0 0 8 0 0
> 
> Thank you very much.
> Excuseme but my mind is out of order.
> Alessandro

y <- x
y[-c(2,5)] <- 0

or

y <- ifelse(1:7 %in% c(2,5), x, 0)

or

y <- rep(0,length(x))
y[c(2,5)] <- x[c(2,5)]

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list