Hi, I have a vector that look like this:> datV1 V2 V3 V4 V5 V6 0.00377467 0.00377467 0.00377467 0.00380083 0.00380083 0.00380083 V7 V8 V9 V10 V11 V12 0.00380959 0.00380959 0.00380959 0.00380083 0.00380083 0.00380083 what I want to do is to insert 0 (zero) for every 3 position yielding: V1 V2 V3 V4 V5 V6 V7 V8 0 0.00377467 0.00377467 0.00377467 0 0.00380083 0.00380083 0.00380083 V9 V10 V11 V12 V13 V14 V15 V16 0 0.00380959 0.00380959 0.00380959 0 .00380083 0.00380083 0.00380083 Is there a quick way to do it in R? - Gundala Viswanath Jakarta - Indonesia
Hi Gundala: Below works but I was trying to figure out a way to not have to add the last line and I gave up. dat<-c(0.00377467,0.00377467,0.00377467,0.00380083,0.00380083,0.00380083,0.00380959, 0.00380959,0.00380959,0.00380083,0.00380083,0.00380083) # MAKE A TEMPORARY MATRIX temp <- matrix(dat,nrow=3) # RBIND THE ZEROS AND MAKE IT A VECTOR withzeros <- as.vector(rbind(temp,0)) # TAKE THE LAST ONE OFF withzeros<-head(withzeros,-1) On Thu, Feb 19, 2009 at 1:47 AM, Gundala Viswanath wrote:> Hi, > > I have a vector that look like this: > >> dat > V1 V2 V3 V4 V5 V6 > 0.00377467 0.00377467 0.00377467 0.00380083 0.00380083 0.00380083 > V7 V8 V9 V10 V11 V12 > 0.00380959 0.00380959 0.00380959 0.00380083 0.00380083 0.00380083 > > > what I want to do is to insert 0 (zero) for every 3 position yielding: > > V1 V2 V3 V4 V5 V6 > V7 V8 > 0 0.00377467 0.00377467 0.00377467 0 0.00380083 0.00380083 > 0.00380083 > V9 V10 V11 V12 V13 V14 > V15 V16 > 0 0.00380959 0.00380959 0.00380959 0 .00380083 0.00380083 0.00380083 > > > Is there a quick way to do it in R? > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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.
Petr Pikal petr.pikal at precheza.cz 724008364, 581252140, 581252257 r-help-bounces at r-project.org napsal dne 19.02.2009 07:47:40:> Hi, > > I have a vector that look like this: > > > dat > V1 V2 V3 V4 V5 V6 > 0.00377467 0.00377467 0.00377467 0.00380083 0.00380083 0.00380083 > V7 V8 V9 V10 V11 V12 > 0.00380959 0.00380959 0.00380959 0.00380083 0.00380083 0.00380083 > > > what I want to do is to insert 0 (zero) for every 3 position yielding: > > V1 V2 V3 V4 V5 V6 > V7 V8 > 0 0.00377467 0.00377467 0.00377467 0 0.00380083 0.00380083 0.00380083 > V9 V10 V11 V12 V13 V14 > V15 V16 > 0 0.00380959 0.00380959 0.00380959 0 .00380083 0.00380083 0.00380083 > > > Is there a quick way to do it in R?Use list y<-rnorm(10) lll<-split(y,0:9 %/% 3) unlist(lapply(lll, function(x) c(0,x))) Regards Petr> > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
How about this:> dat<-c(0.00377467,0.00377467,0.00377467,0.00380083,0.00380083,0.00380083,0.00380959,+ 0.00380959,0.00380959,0.00380083,0.00380083,0.00380083)> dat[seq(1, by=3, to=length(dat))] <- 0 > dat[1] 0.00000000 0.00377467 0.00377467 0.00000000 0.00380083 0.00380083 0.00000000 0.00380959 0.00380959 0.00000000 0.00380083 [12] 0.00380083>On Thu, Feb 19, 2009 at 1:47 AM, Gundala Viswanath <gundalav at gmail.com> wrote:> Hi, > > I have a vector that look like this: > >> dat > V1 V2 V3 V4 V5 V6 > 0.00377467 0.00377467 0.00377467 0.00380083 0.00380083 0.00380083 > V7 V8 V9 V10 V11 V12 > 0.00380959 0.00380959 0.00380959 0.00380083 0.00380083 0.00380083 > > > what I want to do is to insert 0 (zero) for every 3 position yielding: > > V1 V2 V3 V4 V5 V6 > V7 V8 > 0 0.00377467 0.00377467 0.00377467 0 0.00380083 0.00380083 0.00380083 > V9 V10 V11 V12 V13 V14 > V15 V16 > 0 0.00380959 0.00380959 0.00380959 0 .00380083 0.00380083 0.00380083 > > > Is there a quick way to do it in R? > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?