[R] create one bigger matrix with one smaller matrix

2015-12-31 Thread Kathryn Lord
Dear R users,

Suppose that I have a matrix A

A <- matrix(c(1,2,3,4),2,2)
> A
 [,1] [,2]
[1,]13
[2,]24

With this matrix A, I'd like to create bigger one, for example,

  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[,14]
 [1,]131313131 3 1 3
1 3
 [2,]242424242 4 2 4
2 4
 [3,]131313131 3 1 3
1 3
 [4,]242424242 4 2 4
2 4
 [5,]131313131 3 1 3
1 3
 [6,]242424242 4 2 4
2 4
 [7,]131313131 3 1 3
1 3
 [8,]242424242 4 2 4
2 4
 [9,]131313131 3 1 3
1 3
[10,]242424242 4 2 4
2 4
[11,]131313131 3 1 3
1 3
[12,]242424242 4 2 4
2 4
[13,]131313131 3 1 3
1 3
[14,]242424242 4 2 4
2 4


In fact, I want much bigger one. I wonder if there is an elegant way to do
this?

Any suggestions? Thank you!

Best wishes and Happy new year

Kathie

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] elegant way to create a sequence with the 'rep' bulit-in function

2015-05-23 Thread Kathryn Lord
Dear R users,

I'd like to create a sequence/vector, for example,

1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3   4 4 4 4 5 5 5 5 4 4 4
4 5 5 5 5 4 4 4 4 5 5 5 5   6 6 6 7 7 7 8 8 8 9 9 9 6 6 6 7 7 7 8 8 8 9
9 9

So I did like this below.

a - 4
b - 3
c - 2

grp - c( rep(1:b, each=c, times=a), rep(1:c, each=a, times=b)+b, rep(1:a,
each=b, times=c)+b+c )

I wonder if there is a more elegant way to do this?

Any suggestions? Thank you!

Best wishes

Kathie

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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 draw a legend outside of the plot

2015-02-04 Thread Kathryn Lord
Dear R users,

I have three plots, so I tried, for exmple,

par(mfrow=c(2,2))

y1 - rnorm(100)
y2 - rnorm(100)
y3- rnorm(100)

plot(y1);plot(y2);plot(y3)

Here, I'd like to put a legend on the bottom right hand side (empty space).

is it possible?

Thanks for helping,

Kathryn Lord

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] create a function with subset statement

2015-01-28 Thread Kathryn Lord
Dear R experts,

Suppose I have a matrix A below.

a - rep(1:4, each=5)
b - rep(1:5, 4)
c - rnorm(20)

A - cbind(a,b,c)

 A
  a bc
 [1,] 1 1  0.761806718
 [2,] 1 2  0.239734573
 [3,] 1 3 -0.728339238
 [4,] 1 4 -0.121946174
 [5,] 1 5 -0.131909077
 [6,] 2 1 -0.069790098
 [7,] 2 2  1.082671767
 [8,] 2 3 -0.869537195
 [9,] 2 4 -0.417222758
[10,] 2 5 -2.432273481
[11,] 3 1  0.425432121
[12,] 3 2 -2.453299938
[13,] 3 3  0.612125174
[14,] 3 4 -0.005387462
[15,] 3 5  1.911146222
[16,] 4 1  0.161408685
[17,] 4 2  0.567118882
[18,] 4 3 -0.948882839
[19,] 4 4  0.485002340
[20,] 4 5 -0.551981333


With this matrix A, I'd like to create several sub-matrices, for example


B - subset(A, (A[,1] %in% c(1,2)  A[,2] %in% c(1,2)) |
  (A[,1] %in% c(3) A[,2] %in% c(1)  )  |
  (A[,1] %in% c(4) A[,2] %in% c(1:4)) )

 B
  a b  c
 [1,] 1 1  0.7618067
 [2,] 1 2  0.2397346
 [3,] 2 1 -0.0697901
 [4,] 2 2  1.0826718
 [5,] 3 1  0.4254321
 [6,] 4 1  0.1614087
 [7,] 4 2  0.5671189
 [8,] 4 3 -0.9488828
 [9,] 4 4  0.4850023


or

C - subset(A, (A[,1] %in% c(1:4)  A[,2] %in% c(1,2)) )

 C
 a b  c
[1,] 1 1  0.7618067
[2,] 1 2  0.2397346
[3,] 2 1 -0.0697901
[4,] 2 2  1.0826718
[5,] 3 1  0.4254321
[6,] 3 2 -2.4532999
[7,] 4 1  0.1614087
[8,] 4 2  0.5671189


or

D - subset(A, (A[,1] %in% c(1,2)  A[,2] %in% c(1:3)) |
  (A[,1] %in% c(3) A[,2] %in% c(1,2)) )

 D
 a b  c
[1,] 1 1  0.7618067
[2,] 1 2  0.2397346
[3,] 1 3 -0.7283392
[4,] 2 1 -0.0697901
[5,] 2 2  1.0826718
[6,] 2 3 -0.8695372
[7,] 3 1  0.4254321
[8,] 3 2 -2.4532999

and so forth.

I am wondering if I could create matrices B, C, D etc AT ONE TIME. In order
to do that, I guess I need to make a function. unfortunately, I have no
idea how to do that.

Any suggestion will be greatly appreciated.

Kathryn Lord

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] create new matrices with specific patterns

2015-01-26 Thread Kathryn Lord
Dear R users,

Suppose I have a matrix A.

 p - 1:4
 q - 1:5
 P-rep(p, each=5)
 Q-rep(q, 4)

 A - cbind(P,Q)
 A
  P Q
 [1,] 1 1
 [2,] 1 2
 [3,] 1 3
 [4,] 1 4
 [5,] 1 5
 [6,] 2 1
 [7,] 2 2
 [8,] 2 3
 [9,] 2 4
[10,] 2 5
[11,] 3 1
[12,] 3 2
[13,] 3 3
[14,] 3 4
[15,] 3 5
[16,] 4 1
[17,] 4 2
[18,] 4 3
[19,] 4 4
[20,] 4 5



With the matrix A, I'd like to generate new matrices B, ..., E below.

B = A[(3,4), (1,2)]
C = A[(2,2), (1,5), (1,1)]
D = A[(4,2)]
E = A[(3,0), (1,4)]


Matrix B means that first three 'p's (1,2,3) has four 'q's (1,2,3,4) and
the forth 'p' element (4) has two 'q's (1,2); in other words,

Is there the easyiest way to create B,...,E in R?

Actually, the example above is a toy example and the matrix A I have is
around 10,000 by 10,000 and the pattern is also very complicated.

Any suggestion will be greatly appreciated.

Best,

Kathryn Lord

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] create new matrices with specific patterns

2015-01-26 Thread Kathryn Lord
Sorry for inconvenience. For clarification,

The matrix B looks like

 B
  P Q
 [1,] 1 1
 [2,] 1 2
 [3,] 1 3
 [4,] 1 4
 [5,] 2 1
 [6,] 2 2
 [7,] 2 3
 [8,] 2 4
[9,] 3 1
[10,] 3 2
[11,] 3 3
[12,] 3 4
[13,] 4 1
[14,] 4 2

The matrix E is

 E
  P Q
[1,] 4 1
[2,] 4 2
[3,] 4 3
[4,] 4 4



On Tue, Jan 27, 2015 at 11:47 AM, Kathryn Lord kathryn.lord2...@gmail.com
wrote:

 Dear R users,

 Suppose I have a matrix A.

  p - 1:4
  q - 1:5
  P-rep(p, each=5)
  Q-rep(q, 4)
 
  A - cbind(P,Q)
  A
   P Q
  [1,] 1 1
  [2,] 1 2
  [3,] 1 3
  [4,] 1 4
  [5,] 1 5
  [6,] 2 1
  [7,] 2 2
  [8,] 2 3
  [9,] 2 4
 [10,] 2 5
 [11,] 3 1
 [12,] 3 2
 [13,] 3 3
 [14,] 3 4
 [15,] 3 5
 [16,] 4 1
 [17,] 4 2
 [18,] 4 3
 [19,] 4 4
 [20,] 4 5
 


 With the matrix A, I'd like to generate new matrices B, ..., E below.

 B = A[(3,4), (1,2)]
 C = A[(2,2), (1,5), (1,1)]
 D = A[(4,2)]
 E = A[(3,0), (1,4)]


 Matrix B means that first three 'p's (1,2,3) has four 'q's (1,2,3,4) and
 the forth 'p' element (4) has two 'q's (1,2); in other words,

 Is there the easyiest way to create B,...,E in R?

 Actually, the example above is a toy example and the matrix A I have is
 around 10,000 by 10,000 and the pattern is also very complicated.

 Any suggestion will be greatly appreciated.

 Best,

 Kathryn Lord



[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] sum of grouped elements of vector

2015-01-23 Thread Kathryn Lord
Dear R users,

I have a quick quesiton.

Here is a vector a.

a- c(1,2,3,4,5,6,7,8).

(In fact, I have a huge vector.)


With a, I'd like to create new vectors, for example,

new1 = (1+2, 3, 4+5+6, 7+8)
new2 = (1, 2+3+4+5+6+7, 8)
new3 = (1+2+3+4+5+6+7, 8)


How could I make the above vectors using R?


Any suggestion will be greatly appreciated.

Best,

Kathryn Lord

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] create matrices with constraint

2014-12-12 Thread Kathryn Lord
Dear all,

Suppose that I have natural numbers 1 through 28.

Based on these numbers, choose 4 numbers 7 times without replacement and
make a 4 by 7 matrix, for example,

 a1
 [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]159   13   17   21   25
[2,]26   10   14   18   22   26
[3,]37   11   15   19   23   27
[4,]48   12   16   20   24   28

and again create another 4 * 7 matrix, say a2, in the same way; however,
every element of each column in a2 does not exist in any column of a1 like
this, e.g.

 a2
 [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]1234567
[2,]89   10   11   12   13   14
[3,]   15   16   17   18   19   20   21
[4,]   22   23   24   25   26   27   28


and again create another 4 * 7 matrix, say a3, in the same way; however,
every element of each column in a3 does not exist in any column of a1 and
a2.

Using same logic, I'd like to make the matrices (a3, a4, a5) as many as
possible.


Any suggestion will be greatly appreciated.

Best,

Kathryn Lord

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] make matrices as many as possible with a constraint

2014-12-11 Thread Kathryn Lord
Dear R users,

I'd like to make 4 by 7 matrices as many as possible with natural numbers 1
through 28 such that each matrix have different elements of each column.

For example,

simply here is one

 a1 - matrix(1:28, 4,7)
 a1
 [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]159   13   17   21   25
[2,]26   10   14   18   22   26
[3,]37   11   15   19   23   27
[4,]48   12   16   20   24   28

another one

 a2 - matrix(1:28, 4,7, byrow=T)
 a2
 [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]1234567
[2,]89   10   11   12   13   14
[3,]   15   16   17   18   19   20   21
[4,]   22   23   24   25   26   27   28

Matrices a1 and a2 have different columns, and I guess there are such many
matrices.

Any suggestion will be greatly appreciated.

Best,

Kathryn Lord

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] pair-wise computation of columns in a matrix

2014-01-26 Thread Kathryn Lord
Dear R users,

I'd like to compute rho(looks like a correlation matrix) with every two
columns of uu matrix below.


Toy example,


 uu - matrix(1:15, nr=3, nc=5)
 uu
 [,1] [,2] [,3] [,4] [,5]
[1,]147   10   13
[2,]258   11   14
[3,]369   12   15



rho - function(x,y)
{
sum(x*y)/(sqrt(sum(x^2))*sqrt(sum(y^2)))
}

rho_12 - rho(uu[,1],uu[,2])
rho_13 - rho(uu[,1],uu[,3])
rho_14 - rho(uu[,1],uu[,4])
rho_15 - rho(uu[,1],uu[,5])

rho_23 - rho(uu[,2],uu[,3])
rho_24 - rho(uu[,2],uu[,4])
rho_24 - rho(uu[,2],uu[,5])

rho_34 - rho(uu[,3],uu[,4])
rho_35 - rho(uu[,3],uu[,5])

rho_45 - rho(uu[,4],uu[,5])


Actually, the matrix uu is huge, 20*1000.  Would you plz tell me how to
calculate rho, more efficiently??

Any suggestion will be greatly appreciated.

Best,

Kathryn Lord

[[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] replace NA with another vector

2013-12-24 Thread Kathryn Lord
Dear R users,

I have two different vectors like below

x - c( NA, NA, 3, NA, 1)
y - c( 20, 40 ,50)

Combining x and y, I'd like to create new vector z

z - c(20, 40, 3, 50, 1)


Any suggestion will be greatly appreciated.

Best,

Kathryn Lord

[[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] matrix manipulation with its rows

2013-01-16 Thread Kathryn Lord
Dear R users,

I have a question about matrix manipulation with its rows.

Plz see the simple example below


sample - list(matrix(1:6, nr=2,nc=3), matrix(7:12, nr=2,nc=3),
matrix(13:18,nr=2,nc=3))

 sample
[[1]]
 [,1] [,2] [,3]
[1,]135
[2,]246

[[2]]
 [,1] [,2] [,3]
[1,]79   11
[2,]8   10   12

[[3]]
 [,1] [,2] [,3]
[1,]   13   15   17
[2,]   14   16   18

With this list, I'd like to create this below

[[1]]
 [,1] [,2] [,3]  [,4] [,5] [,6]
[1,]135 000
[2,]000   246

[[2]]
 [,1] [,2] [,3]   [,4] [,5] [,6]
[1,]79   11 000
[2,] 0008   10   12

[[3]]
 [,1] [,2] [,3]   [,4]   [,5]   [,6]
[1,]   13   15   17  000
[2,]   000   14   16   18



Any suggestion will be greatly appreciated.

Regards,

Kathryn Lord

[[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] Table to matrix

2010-05-24 Thread Kathryn Lord
Dear R users,

I am trying to make this (3 by 10) matrix A

--A

0   0 00  1  0  0  0  0  0
0   0 00  0  1  0  0  0  0
0   0.5  0.5  0  0  0  0  0  0  0

---

from mass.func

--mass.func---
 mass.func
$`00`
prop
5
1

$`10`
prop
6
1

$`11`
prop
  2   3
0.5 0.5
-

which means that

A[1,5] = 1
A[2,6] =1
A[3,2] = A[3,3] = 0.5

otherwise, zero.

Any suggestion will be greatly appreciated.

Regrads,

Kathryn Lord


p.s.

Here is R code.

--

 dat - as.data.frame(matrix( c( 2, 1, 1, 3, 1, 1, 6, 1, 0, 5, 0, 0), 4, 3,
byrow=T))
  covar - apply(dat[,-1],1,paste,collapse='')
 sp.dat - split(dat,covar)
 y.covar - lapply(sp.dat, [,1)

 prop - function(prop) { table(prop)/sum(table(prop)) }
 mass.func - lapply(y.covar,prop)
 mass.func
$`00`
prop
5
1

$`10`
prop
6
1

$`11`
prop
  2   3
0.5 0.5

[[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.