An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130224/d428cb7e/attachment.pl>
filling an array
5 messages · Jannetta Steyn, Jim Lemon, Joshua Wiley +1 more
On 02/24/2013 02:56 PM, Jannetta Steyn wrote:
Hi All I'm just wondering if there is a quick way of filling a way with the following. I want to declare array I with a specific length and then alternatively fill it with 10 zeros and 10 specified values: v<- 14 I<- c(0,length(t)) But in stead of just filling I with 0 I want 10 zeros and then 10 fourteens (i.e. the value in v) Hope that makes sense.
Hi Janetta,
It makes some sense, but perhaps not enough. The easy answer would be:
I<-c(rep(0,10),rep(v,10))
If you mean you want _alternating_ values:
I<-rep(c(0,14),10)
If you want a general solution to the problem of filling a vector of
arbitrary length with alternating values:
fill_alternating<-function(len=2,value1=0,value2=1) {
return(rep(c(value1,value2),(len+1)/2)[1:len])
}
There are probably other interpretations as well.
Jim
Hi Jannetta, Try this: rep(c(0, v), each = 10) See ?rep for details Cheers, Josh
On Sat, Feb 23, 2013 at 7:56 PM, Jannetta Steyn <jannetta at henning.org> wrote:
Hi All I'm just wondering if there is a quick way of filling a way with the following. I want to declare array I with a specific length and then alternatively fill it with 10 zeros and 10 specified values: v<- 14 I <- c(0,length(t)) But in stead of just filling I with 0 I want 10 zeros and then 10 fourteens (i.e. the value in v) Hope that makes sense. Regards Jannetta -- =================================== Web site: http://www.jannetta.com Email: jannetta at henning.org =================================== [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130224/204a7beb/attachment.pl>
Hi, The usual way is I <- rep(c(0, v), each=10) Best, Ista
On Sat, Feb 23, 2013 at 10:56 PM, Jannetta Steyn <jannetta at henning.org> wrote:
Hi All I'm just wondering if there is a quick way of filling a way with the following. I want to declare array I with a specific length and then alternatively fill it with 10 zeros and 10 specified values: v<- 14 I <- c(0,length(t)) But in stead of just filling I with 0 I want 10 zeros and then 10 fourteens (i.e. the value in v) Hope that makes sense. Regards Jannetta -- =================================== Web site: http://www.jannetta.com Email: jannetta at henning.org =================================== [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.