Re: [R] How to repeat vectors ?

2006-10-02 Thread Tong Wang
Hi, 
Thanks you guys for all the help. I learned a lot from it.  
It looks using apply() is not an efficient way, since all it does is 
looping through 
each row(or col) , which would be slow for large matrix, right ? 

cheers

- Original Message -
From: Gabor Grothendieck [EMAIL PROTECTED]
Date: Saturday, September 30, 2006 4:54 am
Subject: Re: [R] How to repeat vectors ?
To: Tong Wang [EMAIL PROTECTED]
Cc: r-help@stat.math.ethz.ch

 Here are 4 approaches in order from most compact
 to least.  #1 only works for numeric matrices, # 2 is
 a shorter versio of your solution using rep.vec and # 3
 is from Alex's post and is likely what I would
 use in practice.
 
 m - matrix(1:4, 2) # test matrix
 
 # 1 - m must be numeric for this one to work
 kronecker(m, rep(1,2))
 
 # 2
 apply(m, 2, rep, each = 2) # 2
 
 # 3 - from Alex's post
 m[rep(1:nrow(m), each = 2),]
 
 # 4
 matrix(rbind(c(m), c(m)), nc = ncol(m))
 
 On 9/30/06, Tong Wang [EMAIL PROTECTED] wrote:
  I just figured out a way to do this:
   rep.vec - function(X,n)
 return(t(array(rep(X,n),c(length(X),n
Then,apply(MyMatrix, 2, rep.vec,2)
 
  Is there a better way ?  Is there an internal function to repeat 
 a vector or matrix ?
 
  Thanks a lot.
 
 
  - Original Message -
  From: Tong Wang [EMAIL PROTECTED]
  Date: Friday, September 29, 2006 11:23 pm
  Subject: How to repeat vectors ?
  To: r-help@stat.math.ethz.ch
 
   Hi,
  If I have a matrix  , say   a11   a12
 a21  a22
  Is there a routine to get:  a11  a12
   a11  a12
   a21   a22
   a21   a22
  
   Thanks a lot for any help.
  
   best
  
 
  __
  R-help@stat.math.ethz.ch 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.
 


__
R-help@stat.math.ethz.ch 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.


[R] How to repeat vectors ?

2006-09-30 Thread Tong Wang
Hi,
If I have a matrix  , say   a11   a12
   a21  a22
Is there a routine to get:  a11  a12
 a11  a12
 a21   a22
 a21   a22

 Thanks a lot for any help.

best

__
R-help@stat.math.ethz.ch 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.


Re: [R] How to repeat vectors ?

2006-09-30 Thread Tong Wang
I just figured out a way to do this: 
  rep.vec - function(X,n)return(t(array(rep(X,n),c(length(X),n

   Then,apply(MyMatrix, 2, rep.vec,2)

Is there a better way ?  Is there an internal function to repeat a vector or 
matrix ?

Thanks a lot.


- Original Message -
From: Tong Wang [EMAIL PROTECTED]
Date: Friday, September 29, 2006 11:23 pm
Subject: How to repeat vectors ?
To: r-help@stat.math.ethz.ch

 Hi,
If I have a matrix  , say   a11   a12
   a21  a22
Is there a routine to get:  a11  a12
 a11  a12
 a21   a22
 a21   a22
 
 Thanks a lot for any help.
 
 best


__
R-help@stat.math.ethz.ch 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.


Re: [R] How to repeat vectors ?

2006-09-30 Thread Alex Brown
Solution:

m[rep(1:nrow(m),each=2),]

Explanation:

There is a simple and effective way to do this, using array slices.

for your input matrix, m:

  m=matrix(paste(a,c(11,12,21,22),sep=),2)
  m
  [,1]  [,2]
[1,] a11 a21
[2,] a12 a22

you want to create

  [,1]  [,2]
[1,] a11 a21
[2,] a11 a21
[3,] a12 a22
[3,] a12 a22

First, let's just consider the simpler problem of vectors - taking  
the first column as an example:

  v=m[,1]
  v
[1] a11 a12

and you want:

[1] a11 a11 a12 a12

which is the first element, followed by another copy of the first,  
and then the second, followed by another copy of the second, ie:

  v[c(1,1,2,2)]
[1] a11 a11 a12 a12

can we generate the sequence c(1,1,2,2) automatically?  yes:

  rep(c(1,2),each=2)
[1] 1 1 2 2

or:

  rep(1:length(v),each=2)
[1] 1 1 2 2

So let's apply that to the vector:

  v[rep(1:length(v),each=2)]
[1] a11 a11 a12 a12

Going back to the matrix, we can see that we want to do the same  
thing, but to the rows of the matrix, instead of the elements of the  
vector:

Instead of length, we use nrow, and we use the row specifier [r,]

  m[rep(1:nrow(m),each=2),]
  [,1]  [,2]
[1,] a11 a21
[2,] a11 a21
[3,] a12 a22
[4,] a12 a22


-Alex

On 30 Sep 2006, at 07:33, Tong Wang wrote:

 I just figured out a way to do this:
   rep.vec - function(X,n)return(t(array(rep(X,n),c 
 (length(X),n

Then,apply(MyMatrix, 2, rep.vec,2)

 Is there a better way ?  Is there an internal function to repeat a  
 vector or matrix ?

 Thanks a lot.


 - Original Message -
 From: Tong Wang [EMAIL PROTECTED]
 Date: Friday, September 29, 2006 11:23 pm
 Subject: How to repeat vectors ?
 To: r-help@stat.math.ethz.ch

 Hi,
If I have a matrix  , say   a11   a12
   a21  a22
Is there a routine to get:  a11  a12
 a11  a12
 a21   a22
 a21   a22

 Thanks a lot for any help.

 best


 __
 R-help@stat.math.ethz.ch 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.

__
R-help@stat.math.ethz.ch 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.


Re: [R] How to repeat vectors ?

2006-09-30 Thread Adrian DUSA
Maybe this one?

 MyMatrix - matrix(1:4, nrow=2)

 MyMatrix
 [,1] [,2]
[1,]13
[2,]24

 MyMatrix[rep(seq(nrow(MyMatrix)), each=2), ]
 [,1] [,2]
[1,]13
[2,]13
[3,]24
[4,]24


HTH,
Adrian

On Saturday 30 September 2006 09:33, Tong Wang wrote:
 I just figured out a way to do this:
   rep.vec - function(X,n)   
 return(t(array(rep(X,n),c(length(X),n

Then,apply(MyMatrix, 2, rep.vec,2)

 Is there a better way ?  Is there an internal function to repeat a vector
 or matrix ?

 Thanks a lot.


 - Original Message -
 From: Tong Wang [EMAIL PROTECTED]
 Date: Friday, September 29, 2006 11:23 pm
 Subject: How to repeat vectors ?
 To: r-help@stat.math.ethz.ch

  Hi,
 If I have a matrix  , say   a11   a12
a21  a22
 Is there a routine to get:  a11  a12
  a11  a12
  a21   a22
  a21   a22
 
  Thanks a lot for any help.
 
  best

-- 
Adrian DUSA
Arhiva Romana de Date Sociale
Bd. Schitu Magureanu nr.1
050025 Bucuresti sectorul 5
Romania
Tel./Fax: +40 21 3126618 \
  +40 21 3120210 / int.101

__
R-help@stat.math.ethz.ch 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.


Re: [R] How to repeat vectors ?

2006-09-30 Thread Gabor Grothendieck
Here are 4 approaches in order from most compact
to least.  #1 only works for numeric matrices, # 2 is
a shorter versio of your solution using rep.vec and # 3
is from Alex's post and is likely what I would
use in practice.

m - matrix(1:4, 2) # test matrix

# 1 - m must be numeric for this one to work
kronecker(m, rep(1,2))

# 2
apply(m, 2, rep, each = 2) # 2

# 3 - from Alex's post
m[rep(1:nrow(m), each = 2),]

# 4
matrix(rbind(c(m), c(m)), nc = ncol(m))

On 9/30/06, Tong Wang [EMAIL PROTECTED] wrote:
 I just figured out a way to do this:
  rep.vec - function(X,n)return(t(array(rep(X,n),c(length(X),n

   Then,apply(MyMatrix, 2, rep.vec,2)

 Is there a better way ?  Is there an internal function to repeat a vector or 
 matrix ?

 Thanks a lot.


 - Original Message -
 From: Tong Wang [EMAIL PROTECTED]
 Date: Friday, September 29, 2006 11:23 pm
 Subject: How to repeat vectors ?
 To: r-help@stat.math.ethz.ch

  Hi,
 If I have a matrix  , say   a11   a12
a21  a22
 Is there a routine to get:  a11  a12
  a11  a12
  a21   a22
  a21   a22
 
  Thanks a lot for any help.
 
  best
 

 __
 R-help@stat.math.ethz.ch 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.


__
R-help@stat.math.ethz.ch 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.


Re: [R] How to repeat vectors ?

2006-09-30 Thread Gabor Grothendieck
Here are some timings.  From fastest to slowest
we have: #3, #4, #1, #2 so, yes, the apply
approach, even with the improvement (#2), is the
slowest and, in fact, on this test is an order
of magnitude slower than #3 which is the fastest one.

 m - matrix(1:4, 200) # test matrix

 # 1 - m must be numeric for this one to work
 system.time(for(i in 1:100)kronecker(m, rep(1,2)))
[1] 2.93 0.29 3.34   NA   NA

 # 2
 system.time(for(i in 1:100)apply(m, 2, rep, each = 2))
[1] 5.22 0.09 5.73   NA   NA

 # 3 - from Alex's post
 system.time(for(i in 1:100)m[rep(1:nrow(m), each = 2),])
[1] 0.50 0.07 0.60   NA   NA

 # 4
 system.time(for(i in 1:100)matrix(rbind(c(m), c(m)), nc = ncol(m)))
[1] 1.54 0.20 1.77   NA   NA

On 10/1/06, Tong Wang [EMAIL PROTECTED] wrote:
 Hi,
Thanks you guys for all the help. I learned a lot from it.
It looks using apply() is not an efficient way, since all it does is 
 looping through
 each row(or col) , which would be slow for large matrix, right ?

 cheers

 - Original Message -
 From: Gabor Grothendieck [EMAIL PROTECTED]
 Date: Saturday, September 30, 2006 4:54 am
 Subject: Re: [R] How to repeat vectors ?
 To: Tong Wang [EMAIL PROTECTED]
 Cc: r-help@stat.math.ethz.ch

  Here are 4 approaches in order from most compact
  to least.  #1 only works for numeric matrices, # 2 is
  a shorter versio of your solution using rep.vec and # 3
  is from Alex's post and is likely what I would
  use in practice.
 
  m - matrix(1:4, 2) # test matrix
 
  # 1 - m must be numeric for this one to work
  kronecker(m, rep(1,2))
 
  # 2
  apply(m, 2, rep, each = 2) # 2
 
  # 3 - from Alex's post
  m[rep(1:nrow(m), each = 2),]
 
  # 4
  matrix(rbind(c(m), c(m)), nc = ncol(m))
 
  On 9/30/06, Tong Wang [EMAIL PROTECTED] wrote:
   I just figured out a way to do this:
rep.vec - function(X,n)
  return(t(array(rep(X,n),c(length(X),n
 Then,apply(MyMatrix, 2, rep.vec,2)
  
   Is there a better way ?  Is there an internal function to repeat
  a vector or matrix ?
  
   Thanks a lot.
  
  
   - Original Message -
   From: Tong Wang [EMAIL PROTECTED]
   Date: Friday, September 29, 2006 11:23 pm
   Subject: How to repeat vectors ?
   To: r-help@stat.math.ethz.ch
  
Hi,
   If I have a matrix  , say   a11   a12
  a21  a22
   Is there a routine to get:  a11  a12
a11  a12
a21   a22
a21   a22
   
Thanks a lot for any help.
   
best
   
  
   __
   R-help@stat.math.ethz.ch 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.
  
 


__
R-help@stat.math.ethz.ch 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.