[R] summarizing "alternative to rep"
Bill Shipley
Bill.Shipley at Usherbrooke.ca
Thu Nov 14 17:32:13 CET 2002
Yesterday I asked how to "expand" a vector similar to the rep() function
but keeping the original order. For instance, given a vector:
x<-c(3,2,5)
I was looking for some function - call it expand() - such that :
expand(x,3) would return 3,3,3,2,2,2,5,5,5
I received many replies and, as usual in R, there are many different ways
of doing this.
1. Brian D. Ripley said: rep(x, each=) was something from S4 I added to R
for 1.5.0 so it has only been there for 6 months or so. This is the
simplest way but I had an older version of R...
2. rep(c(2,5,4),rep(3,3)) (or more generally: y<-rep(x,rep(length(x),3))
) which also works.
3. I found that this works as well:
as.vector(apply(as.matrix(y),1,function(x){rep(x,n.times)}))
Thanks to all who helped.
Bill Shipley
Departement de biologie
Universite de Sherbrooke
Sherbrooke (Quebec) CANADA J1K 2R9
Bill.Shipley at USherbrooke.ca
http://callisto.si.usherb.ca:8080/bshipley/
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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