Skip to content

Insert value in a Vector Alternately

9 messages · Gundala Viswanath, PIKAL Petr, jim holtman +4 more

#
Hi,

I have a vector that look like this:
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
#
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:
Use list

y<-rnorm(10)
lll<-split(y,0:9 %/% 3)
unlist(lapply(lll, function(x) c(0,x)))

Regards
Petr
http://www.R-project.org/posting-guide.html
#
How about this:
+ 0.00380959,0.00380959,0.00380083,0.00380083,0.00380083)
[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:

  
    
#
Perhaps you can try this,
I don't know how to avoid the transpose operation that might slow  
things down in large cases.

Hope this helps,

baptiste
On 19 Feb 2009, at 12:47, jim holtman wrote:

            
_____________________________

Baptiste Augui?

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag
#
to avoid the transposition you can use

rbind(matrix(d, nrow=3), 0)


baptiste auguie schrieb:

  
    
#
actually

 c(rbind(0,matrix(d, nrow=3)))

which has the bonus of giving the desired result ;)



baptiste auguie schrieb:

  
    
#
thanks all for the correction, funny how it's often the complicated  
solution that comes to mind first.

baptiste
On 19 Feb 2009, at 13:41, Eik Vettorazzi wrote:

            
_____________________________

Baptiste Augui?

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag