Re: [R] Matrix problem

2012-04-10 Thread David Winsemius


On Apr 10, 2012, at 7:33 PM, Worik R wrote:


Friends

I am extracting sub-sets of the rows of a matrix.  Generally the  
result is

a matrix.  But there is a special case.  When the result returned is a
single row it is returned as a vector (in the example below an integer
vector).  If there are 0, or more than 1 rows returned the result is a
matrix.

I am doing this in a function and I cannot be sure how many rows I am
removing.  How can I do this in a general way that always returns a  
matrix?




?[

 M[1, , drop=FALSE]
   a b c d
a1 0 3 2 1
 class( M[1, , drop=FALSE] )
[1] matrix




M - matrix(0:3, nrow=3, ncol=4)
colnames(M) - c('a','b','c','d')
rownames(M) - c('a1','b2','c3')
N - M[M[,a]==0,]
O - M[M[,a]!=0,]
P - M[M[,a]==100,]
c(class(M), class(N), class(O), class(P))

[1] matrix  integer matrix  matrix

M

  a b c d
a1 0 3 2 1
b2 1 0 3 2
c3 2 1 0 3

N

a b c d
0 3 2 1

O

  a b c d
b2 1 0 3 2
c3 2 1 0 3

P

a b c d




cheers
Worik

[[alternative HTML version deleted]]

__
R-help@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.


David Winsemius, MD
West Hartford, CT

__
R-help@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.


Re: [R] Matrix problem

2012-04-10 Thread Worik R
Thank you.

That was exactly what I need.

Looking at '?[' I see...

drop: For matrices and arrays.  If ‘TRUE’ the result is coerced to
  the lowest possible dimension (see the examples).  This only
  works for extracting elements, not for the replacement.  See
  ‘drop’ for further details.


But that implies that in the case where 0 rows are returned it should be
coerced into zero dimensions.  I am not quite sure what it would mean to be
coerced into zero dimensions, but if I read that without having seen the
actual behavior (impossible now) I would assume...

M[M[,a]==1000,] (from my example below)

would return NULL, which has class NULL rather than a matrix with zero
rows.

thanks
Worik

On Wed, Apr 11, 2012 at 11:54 AM, David Winsemius dwinsem...@comcast.netwrote:


 On Apr 10, 2012, at 7:33 PM, Worik R wrote:

  Friends

 I am extracting sub-sets of the rows of a matrix.  Generally the result is
 a matrix.  But there is a special case.  When the result returned is a
 single row it is returned as a vector (in the example below an integer
 vector).  If there are 0, or more than 1 rows returned the result is a
 matrix.

 I am doing this in a function and I cannot be sure how many rows I am
 removing.  How can I do this in a general way that always returns a
 matrix?


 ?[

  M[1, , drop=FALSE]

   a b c d
 a1 0 3 2 1
  class( M[1, , drop=FALSE] )
 [1] matrix


  M - matrix(0:3, nrow=3, ncol=4)
 colnames(M) - c('a','b','c','d')
 rownames(M) - c('a1','b2','c3')
 N - M[M[,a]==0,]
 O - M[M[,a]!=0,]
 P - M[M[,a]==100,]
 c(class(M), class(N), class(O), class(P))

 [1] matrix  integer matrix  matrix

 M

  a b c d
 a1 0 3 2 1
 b2 1 0 3 2
 c3 2 1 0 3

 N

 a b c d
 0 3 2 1

 O

  a b c d
 b2 1 0 3 2
 c3 2 1 0 3

 P

a b c d



 cheers
 Worik

[[alternative HTML version deleted]]

 __**
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/**
 posting-guide.html http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


 David Winsemius, MD
 West Hartford, CT



[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Matrix problem

2012-04-10 Thread David Winsemius

On Apr 10, 2012, at 8:01 PM, Worik R wrote:

 Thank you.

 That was exactly what I need.

 Looking at '?[' I see...

 drop: For matrices and arrays.  If ‘TRUE’ the result is coerced to
   the lowest possible dimension (see the examples).  This only
   works for extracting elements, not for the replacement.  See
   ‘drop’ for further details.


 But that implies that in the case where 0 rows are returned it  
 should be coerced into zero dimensions.  I am not quite sure what it  
 would mean to be coerced into zero dimensions, but if I read that  
 without having seen the actual behavior (impossible now) I would  
 assume...

 M[M[,a]==1000,] (from my example below)

 would return NULL, which has class NULL rather than a matrix with  
 zero rows.

Cue music:
http://www.youtube.com/watch?v=NzlG28B-R8Y

  : That signpost up ahead  ... You're entering into a land of shadow  
and substance. You just crossedoer into the  R Zen Zone.

...   where the sounds of one hand clapping is the test question:

  dim(M[0, ])
[1] 0 4
  str(M[0, ])
  int[0 , 1:4]
  - attr(*, dimnames)=List of 2
   ..$ : NULL
   ..$ : chr [1:4] a b c d
  dim(M[1, ])
NULL


 thanks
 Worik

 On Wed, Apr 11, 2012 at 11:54 AM, David Winsemius dwinsem...@comcast.net 
  wrote:

 On Apr 10, 2012, at 7:33 PM, Worik R wrote:

 Friends

 I am extracting sub-sets of the rows of a matrix.  Generally the  
 result is
 a matrix.  But there is a special case.  When the result returned is a
 single row it is returned as a vector (in the example below an integer
 vector).  If there are 0, or more than 1 rows returned the result is a
 matrix.

 I am doing this in a function and I cannot be sure how many rows I am
 removing.  How can I do this in a general way that always returns a  
 matrix?


 ?[

  M[1, , drop=FALSE]

   a b c d
 a1 0 3 2 1
  class( M[1, , drop=FALSE] )
 [1] matrix


 M - matrix(0:3, nrow=3, ncol=4)
 colnames(M) - c('a','b','c','d')
 rownames(M) - c('a1','b2','c3')
 N - M[M[,a]==0,]
 O - M[M[,a]!=0,]
 P - M[M[,a]==100,]
 c(class(M), class(N), class(O), class(P))
 [1] matrix  integer matrix  matrix
 M
  a b c d
 a1 0 3 2 1
 b2 1 0 3 2
 c3 2 1 0 3
 N
 a b c d
 0 3 2 1
 O
  a b c d
 b2 1 0 3 2
 c3 2 1 0 3
 P
a b c d


 cheers
 Worik

[[alternative HTML version deleted]]

 __
 R-help@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.

 David Winsemius, MD
 West Hartford, CT



David Winsemius, MD
West Hartford, CT


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Matrix problem to extract animal associations

2012-02-27 Thread ilai
set.seed(1)
(DFid - data.frame(
x = sample(1:20,10),
y = sample(1:20,10),
IDs = sapply(1:10,function(i) paste(ID,i,sep=

require(spdep)
coordinates(DFid) - ~x+y
coords - coordinates(DFid)
dnn4 - dnearneigh(DFid,0,4)
 summary(dnn4)
 plot(DFid)
 plot(dnn4,coords,add=T,col=2)
 nb2mat(dnn4, zero.policy=TRUE)

This just one option from the multitude of spatial packages.

HTH


On Sun, Feb 26, 2012 at 4:55 PM, Ross Dwyer ross.dw...@uq.edu.au wrote:
 Dear List,

 I have been trying to extract associations from a matrix whereby individual 
 locations are within a certain distance threshold from one another.

 I have been able to extract those individuals where there is 'no interaction' 
 (i.e. where these individuals are not within a specified distance threshold 
 from another individual) and give these individuals a unique Group ID 
 containing that one individual.

 i.e.

   ID Group

 1 ID1     1

 2 ID3     2

 3 ID4     3

 4 ID5     4

 5 ID7     5

 6 ID8     6

 7 ID9     7


 What I need assistance with is allocating associations with a unique group id.
 i.e. If we have interactions between  ID2_ID6, ID6_ID2, ID6_ID10, 
 ID10_ID6 as in the example code...


   ID Group

 1 ID1     1

 2 ID3     2

 3 ID4     3

 4 ID5     4

 5 ID7     5

 6 ID8     6

 7 ID9     7

 ##
 8 ID2     8
 9 ID6     8
 10 ID10     8

 ##
 The code also needs to robust enough to recognize instances where we have an 
 interaction in a separate group...
 i.e. ID11_ID12 should be in a separate group (Group 9) as they don't 
 interact with IDs 2, 6, or 10 (not in below code!)
 11 ID11     9
 12 ID12     9


 I've been trying to figure this out but have drawn a blank. My example code 
 can be found below.

 Very best wishes,

 Ross

 Dr Ross Dwyer
 Postdoctoral Research Fellow
 University of Queensland



 ###
 require(stats)
 x - sample(1:20,10)
 y - sample(1:20,10)
 IDs - sapply(1:10,function(i) paste(ID,i,sep=))
 (DFid - data.frame(x,y))
    x  y
 1   7 20
 2   5  3
 3  12  5
 4   3 12
 5  18 19
 6   2  1
 7  19 15
 8  20 11
 9  13 14
 10  1  2


 (DMdist - dist(DFid, method = euclidean,
 +                diag = FALSE, upper = TRUE))
           1         2         3         4         5         6         7       
   8         9        10
 1            17.117243 15.811388  8.944272 11.045361 19.646883 13.00 
 15.811388  8.485281 18.973666
 2  17.117243            7.280110  9.219544 20.615528  3.605551 18.439089 
 17.00 13.601471  4.123106
 3  15.811388  7.280110           11.401754 15.231546 10.770330 12.206556 
 10.00  9.055385 11.401754
 4   8.944272  9.219544 11.401754           16.552945 11.045361 16.278821 
 17.029386 10.198039 10.198039
 5  11.045361 20.615528 15.231546 16.552945           24.083189  4.123106  
 8.246211  7.071068 24.041631
 6  19.646883  3.605551 10.770330 11.045361 24.083189           22.022716 
 20.591260 17.029386  1.414214
 7  13.00 18.439089 12.206556 16.278821  4.123106 22.022716            
 4.123106  6.082763 22.203603
 8  15.811388 17.00 10.00 17.029386  8.246211 20.591260  4.123106      
       7.615773 21.023796
 9   8.485281 13.601471  9.055385 10.198039  7.071068 17.029386  6.082763  
 7.615773           16.970563
 10 18.973666  4.123106 11.401754 10.198039 24.041631  1.414214 22.203603 
 21.023796 16.970563

 #Generate True/False matrix on those individuals  4 units apart
 DMTF - apply(as.matrix(DMdist), c(1,2), function(x) ifelse(x=4,T,F))
 diag(DMTF)- NA #replace diagonal with NA
 dimnames(DMTF) - list(IDs, IDs) #add individual's name to matrix

 DMTF
       ID1   ID2   ID3   ID4   ID5   ID6   ID7   ID8   ID9  ID10
 ID1     NA FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 ID2  FALSE    NA FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
 ID3  FALSE FALSE    NA FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 ID4  FALSE FALSE FALSE    NA FALSE FALSE FALSE FALSE FALSE FALSE
 ID5  FALSE FALSE FALSE FALSE    NA FALSE FALSE FALSE FALSE FALSE
 ID6  FALSE  TRUE FALSE FALSE FALSE    NA FALSE FALSE FALSE  TRUE
 ID7  FALSE FALSE FALSE FALSE FALSE FALSE    NA FALSE FALSE FALSE
 ID8  FALSE FALSE FALSE FALSE FALSE FALSE FALSE    NA FALSE FALSE
 ID9  FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE    NA FALSE
 ID10 FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE    NA

 irow - as.character(gl(length(IDs),length(IDs),labels=IDs))
 icol -rep(IDs, length(IDs))

 AssocMatrix - matrix(data=paste(irow,_,icol,_,sep=),
 +                       nrow = length(IDs), ncol = length(IDs),
 +                       dimnames= list(IDs, IDs))

 AssocMatrix

     ID1         ID2         ID3         ID4         ID5         ID6         
 ID7         ID8         ID9         ID10

 ID1  ID1_ID1_  ID2_ID1_  ID3_ID1_  ID4_ID1_  ID5_ID1_  ID6_ID1_  
 ID7_ID1_  ID8_ID1_  ID9_ID1_  ID10_ID1_

 ID2  ID1_ID2_  ID2_ID2_  ID3_ID2_  ID4_ID2_  ID5_ID2_  ID6_ID2_  
 ID7_ID2_  ID8_ID2_  ID9_ID2_  ID10_ID2_

 ID3  ID1_ID3_  ID2_ID3_  ID3_ID3_  ID4_ID3_  ID5_ID3_  ID6_ID3_  
 ID7_ID3_  ID8_ID3_  ID9_ID3_  

Re: [R] matrix problem

2011-06-20 Thread Sarah Goslee
How about:

y - c(1,1,1,3,2)
m - matrix(0, nrow=length(y), ncol=4)
m[y==1, ] - matrix(1:4, nrow=sum(y == 1), ncol=4, byrow=TRUE)

or, depending on your actual problem
y - c(1,1,1,3,2)
m - matrix(0, nrow=length(y), ncol=4)
m[y == 1,] - col(m[y == 1,])

Sarah


On Mon, Jun 20, 2011 at 3:54 PM, Costis Ghionnis conigh...@gmail.com wrote:
 Hallo everyone! I have a problem about creating a matrix...

 Suppose we have a vector y-c(1,1,1,3,2)

 and a zero matrix, m ,with nrows=length(y) and ncol=4.

 The matrix would look like this:
 0       0       0       0
 0       0       0       0
 0       0       0       0
 0       0       0       0
 0       0       0       0

 I want to change the first three rows with the vector c(1,2,3,4).
 I thought that with the command m[y==1,1:4]-c(1,2,3,4) i would get

 1       2       3       4
 1       2       3       4
 1       2       3       4
 0       0       0       0
 0       0       0       0

 but instead i am getting

 1       4       3       2
 2       1       4       3
 3       2       1       4
 0       0       0       0
 0       0       0       0

 It seems it is filling the data by col instead by row. I want to use this 
 technique in more complicated problems.
 So i do not want to have to work with the transpose matrix. Do you know 
 another way to make this work. Thank you...



-- 
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@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.


Re: [R] matrix problem

2011-06-20 Thread David Winsemius


On Jun 20, 2011, at 3:54 PM, Costis Ghionnis wrote:


Hallo everyone! I have a problem about creating a matrix...

Suppose we have a vector y-c(1,1,1,3,2)

and a zero matrix, m ,with nrows=length(y) and ncol=4.

The matrix would look like this:
0   0   0   0
0   0   0   0
0   0   0   0
0   0   0   0
0   0   0   0

I want to change the first three rows with the vector c(1,2,3,4).
I thought that with the command m[y==1,1:4]-c(1,2,3,4) i would get

1   2   3   4
1   2   3   4
1   2   3   4
0   0   0   0
0   0   0   0


Try:

 m[y==1,1:4]-rep( c(1,2,3,4), each= sum(y==1) )
 m
 [,1] [,2] [,3] [,4]
[1,]1234
[2,]1234
[3,]1234
[4,]0000
[5,]0000

--  
David.


but instead i am getting

1   4   3   2
2   1   4   3
3   2   1   4
0   0   0   0
0   0   0   0

It seems it is filling the data by col instead by row. I want to use  
this technique in more complicated problems.
So i do not want to have to work with the transpose matrix. Do you  
know another way to make this work. Thank you...


__
R-help@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.


David Winsemius, MD
West Hartford, CT

__
R-help@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.


Re: [R] matrix problem

2010-08-10 Thread Henrique Dallazuanna
Try this:

Lines - '1 2 0.1
2 3 0.2
3 1 0.3'

DF - read.table(textConnection(Lines))
m - matrix(0, ncol = nrow(DF), nrow = nrow(DF))
m[as.matrix(DF[1:2])] - DF[[3]]

On Tue, Aug 10, 2010 at 3:03 PM, zhenjiang xu zhenjiang...@gmail.comwrote:

 Hi,

 I have a file like this:
 1 2 0.1
 2 3 0.2
 3 1 0.3

 And I want to read it to create a matrix like this:
 [,1] [,2][,3]
 [1,]0   0.1 0
 [2,]0   00.2
 [3,]0.300

 How can I do it efficiently? Thanks.
 --
 Best,
 Zhenjiang

[[alternative HTML version deleted]]

 __
 R-help@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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] matrix problem

2010-08-10 Thread Xia.Li

Let me give you a not that efficient one...

assume you have read the matrix (named as x) into R:

n=dim(x)[1]
y=matrix(0,n,n)
for (i in 1:n) y[x[i,1],x[i,2]]=x[i,3]


-- 
View this message in context: 
http://r.789695.n4.nabble.com/matrix-problem-tp2320193p2320219.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] matrix problem

2010-08-10 Thread William Dunlap

 -Original Message-
 From: r-help-boun...@r-project.org 
 [mailto:r-help-boun...@r-project.org] On Behalf Of zhenjiang xu
 Sent: Tuesday, August 10, 2010 11:03 AM
 To: R-help@r-project.org
 Subject: [R] matrix problem
 
 Hi,
 
 I have a file like this:
 1 2 0.1
 2 3 0.2
 3 1 0.3
 
 And I want to read it to create a matrix like this:
  [,1] [,2][,3]
 [1,]0   0.1 0
 [2,]0   00.2
 [3,]0.300
 
 How can I do it efficiently? Thanks.

Use a k-column matrix as a subscript into your
k-dimensional output array.  (k is 2 in your case.)

E.g., 'input' is your matrix in a form that one
can paste into an R session:
   input - cbind(c(1,2,3), c(2,3,1), c(.1,.2,.3))
   size - max(input[,1:2]) # you may want something else here
   output - matrix(0.0, size, size)
   output[input[,1:2]] - input[,3]
   output
   [,1] [,2] [,3]
  [1,]  0.0  0.1  0.0
  [2,]  0.0  0.0  0.2
  [3,]  0.3  0.0  0.0

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

 -- 
 Best,
 Zhenjiang
 
   [[alternative HTML version deleted]]
 
 __
 R-help@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.
 

__
R-help@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.


Re: [R] matrix problem

2010-08-10 Thread Wu Gong

Hi,

I guess you just want to reshape your data to wide format.

strs - Index Time Value
1 2 0.1
2 3 0.2
3 1 0.3
DF - read.table(textConnection(strs),header=T)
rDF - reshape(DF, idvar=Index, timevar=Time, direction=wide) 
rDF[is.na(rDF)] - 0

-
A R learner.
-- 
View this message in context: 
http://r.789695.n4.nabble.com/matrix-problem-tp2320193p2320287.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] matrix problem

2009-07-05 Thread William Simpson
Thanks everyone for your help!

Bill

__
R-help@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.


Re: [R] matrix problem

2009-07-04 Thread Henrique Dallazuanna
Try this:

m - matrix(0, nrow = 5, ncol = 4)
diag(m) - x[1]
diag(m[-1,]) - x[2]

On Sat, Jul 4, 2009 at 12:17 PM, William Simpson 
william.a.simp...@gmail.com wrote:

 Can anybody please tell me a good way to do the following?

 Given a vector, number of rows and number of columns, return a matrix
 as follows. Easiest to give an example:

 x=c(1,2), nrow=5, ncol=4

 return the matrix:

   1 0 0 0
   2 1 0 0
   0 2 1 0
   0 0 2 1
   0 0 0 2

 Thanks very much for any help!
 Bill

 __
 R-help@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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] matrix problem

2009-07-04 Thread David Winsemius


On Jul 4, 2009, at 11:17 AM, William Simpson wrote:


Can anybody please tell me a good way to do the following?

Given a vector, number of rows and number of columns, return a matrix
as follows. Easiest to give an example:

x=c(1,2), nrow=5, ncol=4


You ought to separate those assignments with semicolons:

x=c(1,2); nrow=5; ncol=4
 MM - matrix( c(x,rep(0, nrow-length(x)+1)),  nrow=nrow, ncol=ncol)
Warning message:
In matrix(c(x, rep(0, nrow - length(x) + 1)), nrow = nrow, ncol =  
ncol) :
  data length [6] is not a sub-multiple or multiple of the number of  
rows [5]

 MM
 [,1] [,2] [,3] [,4]
[1,]1000
[2,]2100
[3,]0210
[4,]0021
[5,]0002

OR--

 M2 - diag( , nrow=5, ncol=4)
 M2[row(M2) == col(M2)+1] - 2
 M2
 [,1] [,2] [,3] [,4]
[1,]1000
[2,]2100
[3,]0210
[4,]0021
[5,]0002



return the matrix:

  1 0 0 0
  2 1 0 0
  0 2 1 0
  0 0 2 1
  0 0 0 2

Thanks very much for any help!
Bill

__


David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
R-help@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.


Re: [R] matrix problem

2009-07-04 Thread William Simpson
Thanks everyone for the help.

I should have said that I want to do this generally, not as a one-off.
So I want a function to do it. Like this

tp-function(x, nr, nc)
{
matrix( c(x,rep(0, nr-length(x)+1)),  nrow=nr, ncol=nc)
}

tp(x=c(1,2), nr=5, nc=4)

This one looks good -- the warning message is annoying though...


Bill

__
R-help@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.


Re: [R] matrix problem

2009-07-04 Thread Charles C. Berry

On Sat, 4 Jul 2009, William Simpson wrote:


Can anybody please tell me a good way to do the following?


Not sure how you want cases like x = 1:3, nrow=2 , ncol=7 to be handled, 
but for the example you give, this works:


mat - matrix(0,nr=nrow,nc=ncol)
indx - outer(
seq( from=0, length=length(x) ),
seq( from=1, by=nrow+1, length=ncol ), + )
mat[ indx ] - x


HTH,

Chuck



Given a vector, number of rows and number of columns, return a matrix
as follows. Easiest to give an example:

x=c(1,2), nrow=5, ncol=4

return the matrix:

  1 0 0 0
  2 1 0 0
  0 2 1 0
  0 0 2 1
  0 0 0 2

Thanks very much for any help!
Bill

__
R-help@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.



Charles C. Berry(858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cbe...@tajo.ucsd.edu   UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

__
R-help@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.


Re: [R] matrix problem

2009-07-04 Thread Jorge Ivan Velez
Dear William,

Here is one way using Henrique's solution:

Make - function(x, nR, nC){
 m - matrix(0, nrow = nR, ncol = nC)
 diag(m) - x[1]
 diag(m[-1,]) - x[2]
 m
}

Make(x = c(1,2), nR = 5, nC = 4)

HTH,

Jorge


On Sat, Jul 4, 2009 at 11:59 AM, William Simpson 
william.a.simp...@gmail.com wrote:

 Thanks everyone for the help.

 I should have said that I want to do this generally, not as a one-off.
 So I want a function to do it. Like this

 tp-function(x, nr, nc)
 {
 matrix( c(x,rep(0, nr-length(x)+1)),  nrow=nr, ncol=nc)
 }

 tp(x=c(1,2), nr=5, nc=4)

 This one looks good -- the warning message is annoying though...


 Bill

 __
 R-help@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.


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] matrix problem

2009-07-04 Thread David Winsemius


On Jul 4, 2009, at 11:59 AM, William Simpson wrote:


Thanks everyone for the help.

I should have said that I want to do this generally, not as a one-off.
So I want a function to do it. Like this

tp-function(x, nr, nc)
{
matrix( c(x,rep(0, nr-length(x)+1)),  nrow=nr, ncol=nc)
}

tp(x=c(1,2), nr=5, nc=4)

This one looks good -- the warning message is annoying though...


 tp-function(x, nr, nc)
+ {suppressWarnings(
+ matrix( c(x,rep(0, nr-length(x)+1)),  nrow=nr, ncol=nc) )
+ }

 tp(x=c(1,2), nr=5, nc=4)
 [,1] [,2] [,3] [,4]
[1,]1000
[2,]2100
[3,]0210
[4,]0021
[5,]0002

--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
R-help@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.


Re: [R] matrix problem

2009-07-04 Thread William Simpson
Doesn't work:

 Make(x=c(2,1,1,1),nR=5,nC=2)
 [,1] [,2]
[1,]20
[2,]12
[3,]01
[4,]00
[5,]00

should be
 [,1] [,2]
[1,]20
[2,]12
[3,]11
[4,]11
[5,]01



On Sat, Jul 4, 2009 at 5:04 PM, Jorge Ivan
Velezjorgeivanve...@gmail.com wrote:
 Dear William,

 Here is one way using Henrique's solution:

 Make - function(x, nR, nC){
  m - matrix(0, nrow = nR, ncol = nC)
  diag(m) - x[1]
  diag(m[-1,]) - x[2]
  m
 }

 Make(x = c(1,2), nR = 5, nC = 4)

 HTH,

 Jorge


 On Sat, Jul 4, 2009 at 11:59 AM, William Simpson
 william.a.simp...@gmail.com wrote:

 Thanks everyone for the help.

 I should have said that I want to do this generally, not as a one-off.
 So I want a function to do it. Like this

 tp-function(x, nr, nc)
 {
 matrix( c(x,rep(0, nr-length(x)+1)),  nrow=nr, ncol=nc)
 }

 tp(x=c(1,2), nr=5, nc=4)

 This one looks good -- the warning message is annoying though...


 Bill

 __
 R-help@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.



__
R-help@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.


Re: [R] matrix problem

2009-07-04 Thread Henrique Dallazuanna
Try this:

foo - function(x, nrow, ncol){
m - matrix(0, nrow = nrow, ncol = ncol)
m[cbind(unlist(lapply(0:(ncol - 1), `+`, seq(x))),rep(1:ncol, each =
length(x)))] - x
m
}

foo(c(2, 1, 1, 1), nrow = 5, ncol = 2)


On Sat, Jul 4, 2009 at 1:26 PM, William Simpson william.a.simp...@gmail.com
 wrote:

 Doesn't work:

  Make(x=c(2,1,1,1),nR=5,nC=2)
 [,1] [,2]
 [1,]20
 [2,]12
 [3,]01
 [4,]00
 [5,]00

 should be
 [,1] [,2]
 [1,]20
 [2,]12
 [3,]11
 [4,]11
 [5,]01



 On Sat, Jul 4, 2009 at 5:04 PM, Jorge Ivan
 Velezjorgeivanve...@gmail.com wrote:
  Dear William,
 
  Here is one way using Henrique's solution:
 
  Make - function(x, nR, nC){
   m - matrix(0, nrow = nR, ncol = nC)
   diag(m) - x[1]
   diag(m[-1,]) - x[2]
   m
  }
 
  Make(x = c(1,2), nR = 5, nC = 4)
 
  HTH,
 
  Jorge
 
 
  On Sat, Jul 4, 2009 at 11:59 AM, William Simpson
  william.a.simp...@gmail.com wrote:
 
  Thanks everyone for the help.
 
  I should have said that I want to do this generally, not as a one-off.
  So I want a function to do it. Like this
 
  tp-function(x, nr, nc)
  {
  matrix( c(x,rep(0, nr-length(x)+1)),  nrow=nr, ncol=nc)
  }
 
  tp(x=c(1,2), nr=5, nc=4)
 
  This one looks good -- the warning message is annoying though...
 
 
  Bill
 
  __
  R-help@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.
 
 

 __
 R-help@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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Matrix: Problem with the code

2009-01-09 Thread Sarah Goslee
Well, mat doesn't have any dimensions / isn't a matrix, and we don't
know what p is supposed to be. But leaving aside those little details,
do you perhaps want something like this:

  x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
  p - 5
  mat- matrix(0, nrow=p, ncol=length(x))
  for(j in 1:length(x))
  {
  for(i in 1:p)
  mat[i,j]-x[j]^i
  }

Two notes: I didn't try it out, and if that's what you want rather
than a toy example
of a larger problem, there are more elegant ways to do it in R.

Sarah

On Fri, Jan 9, 2009 at 6:42 PM, Bhargab Chattopadhyay
bharga...@yahoo.com wrote:
 Hi,


 Can any one please explain why the following code doesn't work? Or can anyone 
 suggest an alternative.
 Suppose
   x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
mat-0;
for(j in 1:length(x))
{
   for(i in 1:p)
mat[i,j]-x[j]^i;
}
Actually I want to have a matrix with p columns such that each column will 
 have the elements of  x^(column#).

 Thanks in advance.

 Bhargab






-- 
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@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.


Re: [R] Matrix: Problem with the code

2009-01-09 Thread Charlotte Wickham
One of those more elegant ways:
outer(x, 1:p, ^)

Charlotte

On Fri, Jan 9, 2009 at 4:24 PM, Sarah Goslee sarah.gos...@gmail.com wrote:
 Well, mat doesn't have any dimensions / isn't a matrix, and we don't
 know what p is supposed to be. But leaving aside those little details,
 do you perhaps want something like this:

  x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
  p - 5
  mat- matrix(0, nrow=p, ncol=length(x))
  for(j in 1:length(x))
  {
  for(i in 1:p)
  mat[i,j]-x[j]^i
  }

 Two notes: I didn't try it out, and if that's what you want rather
 than a toy example
 of a larger problem, there are more elegant ways to do it in R.

 Sarah

 On Fri, Jan 9, 2009 at 6:42 PM, Bhargab Chattopadhyay
 bharga...@yahoo.com wrote:
 Hi,


 Can any one please explain why the following code doesn't work? Or can 
 anyone suggest an alternative.
 Suppose
   x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
mat-0;
for(j in 1:length(x))
{
   for(i in 1:p)
mat[i,j]-x[j]^i;
}
Actually I want to have a matrix with p columns such that each column 
 will have the elements of  x^(column#).

 Thanks in advance.

 Bhargab






 --
 Sarah Goslee
 http://www.functionaldiversity.org

 __
 R-help@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.


__
R-help@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.


Re: [R] Matrix: Problem with the code

2009-01-09 Thread markleeds
Charlotte: I ran your code because I wasn't clear on it and your way 
would cause more matrices than the person requested. So
I think the code below it, although not too short, does what the person 
asked. Thanks though because I understand outer better now.


temp - matrix(c(1,2,3,4,5,6),ncol=2)
print(temp)

#One of those more elegant ways:
print(temp)
outer(temp,1:p,'^')One of those more elegant ways:


# THIS WAY I THINK GIVES WHAT THEY WANT

sapply(1:ncol(temp), function(.col) {
  temp[,.col]^.col
})



On Fri, Jan 9, 2009 at  7:40 PM, Charlotte Wickham wrote:


One of those more elegant ways:
outer(x, 1:p, ^)

Charlotte

On Fri, Jan 9, 2009 at 4:24 PM, Sarah Goslee sarah.gos...@gmail.com 
wrote:

Well, mat doesn't have any dimensions / isn't a matrix, and we don't
know what p is supposed to be. But leaving aside those little 
details,

do you perhaps want something like this:

 x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
 p - 5
 mat- matrix(0, nrow=p, ncol=length(x))
 for(j in 1:length(x))
 {
 for(i in 1:p)
 mat[i,j]-x[j]^i
 }

Two notes: I didn't try it out, and if that's what you want rather
than a toy example
of a larger problem, there are more elegant ways to do it in R.

Sarah

On Fri, Jan 9, 2009 at 6:42 PM, Bhargab Chattopadhyay
bharga...@yahoo.com wrote:

Hi,


Can any one please explain why the following code doesn't work? Or 
can anyone suggest an alternative.

Suppose
  x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
   mat-0;
   for(j in 1:length(x))
   {
  for(i in 1:p)
   mat[i,j]-x[j]^i;
   }
   Actually I want to have a matrix with p columns such that each 
column will have the elements of  x^(column#).


Thanks in advance.

Bhargab







--
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@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.



__
R-help@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.


__
R-help@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.


Re: [R] Matrix: Problem with the code

2009-01-09 Thread Kingsford Jones
On Fri, Jan 9, 2009 at 6:36 PM,  markle...@verizon.net wrote:
 Charlotte: I ran your code because I wasn't clear on it and your way would
 cause more matrices than the person requested.

Bhargab gave us

 x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)

and said: I want to have a matrix with p columns such that each
column will have the elements of  x^(column#).

so, I think Charlotte's code was spot-on:

p - 3
outer(x, 1:p, '^')
 [,1] [,2]   [,3]
 [1,]   23  529  12167
 [2,]   67 4489 300763
 [3,]24  8
 [4,]   87 7569 658503
 [5,]9   81729
 [6,]   63 3969 250047
 [7,]8   64512
 [8,]24  8
 [9,]   35 1225  42875
[10,]6   36216
[11,]   91 8281 753571
[12,]   41 1681  68921
[13,]   22  484  10648
[14,]39 27


Here's another way -- a bit less elegant, but a gentle
introduction to thinking in vectors rather than elements:

 mat - matrix(0,nrow=length(x), ncol=p)

 for(i in 1:p) mat[,i] - x^i
 mat
 [,1] [,2]   [,3]
 [1,]   23  529  12167
 [2,]   67 4489 300763
 [3,]24  8
 [4,]   87 7569 658503
 [5,]9   81729
 [6,]   63 3969 250047
 [7,]8   64512
 [8,]24  8
 [9,]   35 1225  42875
[10,]6   36216
[11,]   91 8281 753571
[12,]   41 1681  68921
[13,]   22  484  10648
[14,]39 27


best,

Kingsford Jones






So
 I think the code below it, although not too short, does what the person
 asked. Thanks though because I understand outer better now.

 temp - matrix(c(1,2,3,4,5,6),ncol=2)
 print(temp)

 #One of those more elegant ways:
 print(temp)
 outer(temp,1:p,'^')One of those more elegant ways:


 # THIS WAY I THINK GIVES WHAT THEY WANT

 sapply(1:ncol(temp), function(.col) {
  temp[,.col]^.col
 })



 On Fri, Jan 9, 2009 at  7:40 PM, Charlotte Wickham wrote:

 One of those more elegant ways:
 outer(x, 1:p, ^)

 Charlotte

 On Fri, Jan 9, 2009 at 4:24 PM, Sarah Goslee sarah.gos...@gmail.com
 wrote:

 Well, mat doesn't have any dimensions / isn't a matrix, and we don't
 know what p is supposed to be. But leaving aside those little details,
 do you perhaps want something like this:

 x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
 p - 5
 mat- matrix(0, nrow=p, ncol=length(x))
 for(j in 1:length(x))
 {
 for(i in 1:p)
 mat[i,j]-x[j]^i
 }

 Two notes: I didn't try it out, and if that's what you want rather
 than a toy example
 of a larger problem, there are more elegant ways to do it in R.

 Sarah

 On Fri, Jan 9, 2009 at 6:42 PM, Bhargab Chattopadhyay
 bharga...@yahoo.com wrote:

 Hi,


 Can any one please explain why the following code doesn't work? Or can
 anyone suggest an alternative.
 Suppose
  x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
   mat-0;
   for(j in 1:length(x))
   {
  for(i in 1:p)
   mat[i,j]-x[j]^i;
   }
   Actually I want to have a matrix with p columns such that each column
 will have the elements of  x^(column#).

 Thanks in advance.

 Bhargab






 --
 Sarah Goslee
 http://www.functionaldiversity.org

 __
 R-help@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.


 __
 R-help@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.

 __
 R-help@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.


__
R-help@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.


Re: [R] Matrix: Problem with the code

2009-01-09 Thread markleeds
Thanks Kingsford. I thought the column power was supposed to be just for 
that column but you're probably correct.  English has its oddities 
because if one reads the actual sentence the person wrote it's still not 
clear, atleast to me.


Actually I want to have a matrix with p columns such that each column
will have the elements of  x^(column#)

Thanks and apologies to Charlotte for my incorrect correction.



On Fri, Jan 9, 2009 at  9:37 PM, Kingsford Jones wrote:


On Fri, Jan 9, 2009 at 6:36 PM,  markle...@verizon.net wrote:
Charlotte: I ran your code because I wasn't clear on it and your way 
would

cause more matrices than the person requested.


Bhargab gave us

 x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)

and said: I want to have a matrix with p columns such that each
column will have the elements of  x^(column#).

so, I think Charlotte's code was spot-on:

p - 3
outer(x, 1:p, '^')
 [,1] [,2]   [,3]
 [1,]   23  529  12167
 [2,]   67 4489 300763
 [3,]24  8
 [4,]   87 7569 658503
 [5,]9   81729
 [6,]   63 3969 250047
 [7,]8   64512
 [8,]24  8
 [9,]   35 1225  42875
[10,]6   36216
[11,]   91 8281 753571
[12,]   41 1681  68921
[13,]   22  484  10648
[14,]39 27


Here's another way -- a bit less elegant, but a gentle
introduction to thinking in vectors rather than elements:

 mat - matrix(0,nrow=length(x), ncol=p)

 for(i in 1:p) mat[,i] - x^i
 mat
 [,1] [,2]   [,3]
 [1,]   23  529  12167
 [2,]   67 4489 300763
 [3,]24  8
 [4,]   87 7569 658503
 [5,]9   81729
 [6,]   63 3969 250047
 [7,]8   64512
 [8,]24  8
 [9,]   35 1225  42875
[10,]6   36216
[11,]   91 8281 753571
[12,]   41 1681  68921
[13,]   22  484  10648
[14,]39 27


best,

Kingsford Jones






So
I think the code below it, although not too short, does what the 
person

asked. Thanks though because I understand outer better now.

temp - matrix(c(1,2,3,4,5,6),ncol=2)
print(temp)

#One of those more elegant ways:
print(temp)
outer(temp,1:p,'^')One of those more elegant ways:


# THIS WAY I THINK GIVES WHAT THEY WANT

sapply(1:ncol(temp), function(.col) {
 temp[,.col]^.col
})



On Fri, Jan 9, 2009 at  7:40 PM, Charlotte Wickham wrote:


One of those more elegant ways:
outer(x, 1:p, ^)

Charlotte

On Fri, Jan 9, 2009 at 4:24 PM, Sarah Goslee 
sarah.gos...@gmail.com

wrote:


Well, mat doesn't have any dimensions / isn't a matrix, and we 
don't
know what p is supposed to be. But leaving aside those little 
details,

do you perhaps want something like this:

x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
p - 5
mat- matrix(0, nrow=p, ncol=length(x))
for(j in 1:length(x))
{
for(i in 1:p)
mat[i,j]-x[j]^i
}

Two notes: I didn't try it out, and if that's what you want rather
than a toy example
of a larger problem, there are more elegant ways to do it in R.

Sarah

On Fri, Jan 9, 2009 at 6:42 PM, Bhargab Chattopadhyay
bharga...@yahoo.com wrote:


Hi,


Can any one please explain why the following code doesn't work? Or 
can

anyone suggest an alternative.
Suppose
 x-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
  mat-0;
  for(j in 1:length(x))
  {
 for(i in 1:p)
  mat[i,j]-x[j]^i;
  }
  Actually I want to have a matrix with p columns such that each 
column

will have the elements of  x^(column#).

Thanks in advance.

Bhargab







--
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@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.



__
R-help@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.


__
R-help@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.



__
R-help@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.


Re: [R] matrix problem

2008-04-21 Thread Chuck Cleland
On 4/21/2008 5:54 AM, William Simpson wrote:
 Hi Everyone,
 
 I am running into a problem with matrices. I use R version 2.4.1 and
 an older version.
 
 The problem is this:
 m-matrix(ncol=3,nrow=4)
 m[,1:3]-runif(n=4)
 
 That does what I expect; it fills up the rows of the matrix with the
 data vector
 m
   [,1]  [,2]  [,3]
 [1,] 0.2083071 0.2083071 0.2083071
 [2,] 0.5865763 0.5865763 0.5865763
 [3,] 0.7901782 0.7901782 0.7901782
 [4,] 0.8298317 0.8298317 0.8298317
 
 But this doesn't work:
 m[1:4,]-runif(n=3)
 m
[,1]   [,2]   [,3]
 [1,] 0.96864939 0.11656740 0.06182311
 [2,] 0.11656740 0.06182311 0.96864939
 [3,] 0.06182311 0.96864939 0.11656740
 [4,] 0.96864939 0.11656740 0.06182311
 
 I want it to fill up the columns of the matrix with the data vector.

   Does this help?

  matrix(runif(4), ncol=3, nrow=4)
[,1]   [,2]   [,3]
[1,] 0.60226296 0.60226296 0.60226296
[2,] 0.74104084 0.74104084 0.74104084
[3,] 0.70955138 0.70955138 0.70955138
[4,] 0.03136881 0.03136881 0.03136881

  matrix(runif(3), ncol=3, nrow=4, byrow=TRUE)
   [,1]  [,2]  [,3]
[1,] 0.7008625 0.8348078 0.1003123
[2,] 0.7008625 0.8348078 0.1003123
[3,] 0.7008625 0.8348078 0.1003123
[4,] 0.7008625 0.8348078 0.1003123

 Maybe there is a better way to do what I want. I need to do both of
 the above. The matrices are large, so I need a fast method.
 
 Thanks very much for any help.
 
 Bill Simpson
 
 __
 R-help@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.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894

__
R-help@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.


Re: [R] matrix problem

2008-04-21 Thread Rolf Turner

On 21/04/2008, at 9:54 PM, William Simpson wrote:

 Hi Everyone,

 I am running into a problem with matrices. I use R version 2.4.1 and
 an older version.

 The problem is this:
 m-matrix(ncol=3,nrow=4)
 m[,1:3]-runif(n=4)

 That does what I expect; it fills up the rows of the matrix with the
 data vector
 m
   [,1]  [,2]  [,3]
 [1,] 0.2083071 0.2083071 0.2083071
 [2,] 0.5865763 0.5865763 0.5865763
 [3,] 0.7901782 0.7901782 0.7901782
 [4,] 0.8298317 0.8298317 0.8298317

 But this doesn't work:
 m[1:4,]-runif(n=3)
 m
[,1]   [,2]   [,3]
 [1,] 0.96864939 0.11656740 0.06182311
 [2,] 0.11656740 0.06182311 0.96864939
 [3,] 0.06182311 0.96864939 0.11656740
 [4,] 0.96864939 0.11656740 0.06182311

 I want it to fill up the columns of the matrix with the data vector.

 Maybe there is a better way to do what I want. I need to do both of
 the above. The matrices are large, so I need a fast method.

R fills arrays in ``reverse odometer order'' --- the first index (row
index in matrices) ticks over fastest; the second index second fastest.
Unless you tell it to do otherwise.

In other words matrices get filled up column by column.  Unless other
instructions are given.

Thus m[] - runif(3) (note that the ``1:4'' is redundant) puts
the generated 3-vector into the first 3 entries of column 1, then
starts over again, putting the first entry of that vector into the
last entry of column 1, then the next two entries of the vector
into the first two entries of column 2, and so on.

What you want to do is

m - matrix(runif(3),nrow=4,ncol=3,byrow=TRUE)

i.e. it is unnecessary and counter-productive to create m beforehand
and then try to fill it up.

cheers,

Rolf Turner

##
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

__
R-help@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.


Re: [R] matrix problem

2008-04-21 Thread William Simpson
Thanks very much Petr and Rold for your helpful replies.

Cheers
Bill

__
R-help@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.